Git

What Does “git rev-parse” Do?

On Git, developers can get the SHA hash of the current HEAD pointing, remote branch, or working branch name by using the “$ git rev-parse” command. This command can be used along with several options to perform previously described operations, such as “–short”, “–abbrev-ref” and many more.

In this article, we will discuss:

What Does “git rev-parse” Do?

When developers want to print the SHA hash of HEAD where it is pointing or need to get the current working branch name, the “$ git rev-parse” command can be utilized.

Let’s check out the multiple usages of the “$ git rev-parse” command!

How to Get HEAD SHA Hash Using “$ git rev-parse” Command?

The “git rev-parse” command can get the SHA hash where the HEAD is currently pointing. To do so, navigate to the Git root directory by running the “cd” command:

$ cd "C:\Users\nazma\Git"

To get the SHA hash of HEAD’s current position, execute the “git rev-parse” command:

$ git rev-parse HEAD

As you can see, the current HEAD position SHA hash is displayed:

How to Get HEAD Short SHA Hash Using “$ git rev-parse” Command?

If you want to get the HEAD short SHA hash, the following command can be used:

$ git rev-parse --short HEAD

It can be seen that the short SHA hash of HEAD’s current position is shown:

How to Get Remote Branch SHA Hash Using “$ git rev-parse” Command?

The other way of using the “git rev-parse” command is to get the HEAD’s current working branch. For this purpose, first, get the list of all remote and local branches:

$ git branch -a

In the below-given output, the highlighted branches are remote branches. Select the desired branch from the list:

Then, execute the “git rev-parse” command along with the particular remote branch name:

$ git rev-parse origin/dev

As a result, the specified remote branch SHA hash is displayed:

How to Get Current Working Branch Using “$ git rev-parse” Command?

By using the “git rev-parse” command, developers can get the current working branch name:

$ git rev-parse --abbrev-ref HEAD

Here, the “–abbrev-ref” flag will display the local branch name where the HEAD is pointing:

We have briefly explained the usage of the “$ git rev-parse” command along with several options.

Conclusion

The “$ git rev-parse” command can be utilized for getting the SHA hashes of branches or HEAD. For instance, the “$ git rev-parse HEAD” command can be used to get the HEAD SHA hash. The “$ git rev-parse <remote-branch>” command can print the specified branch SHA hash. By using the “$ git rev-parse –abbrev-ref HEAD”, users can get the HEAD working branch. This article illustrated the multiple usages of the $ git rev-parse command.

About the author

Maria Naz

I hold a master's degree in computer science. I am passionate about my work, exploring new technologies, learning programming languages, and I love to share my knowledge with the world.