In this article, we will discuss:
- What Does “git rev-parse” Do?
- How to Get HEAD SHA Hash Using “$ git rev-parse” Command?
- How to Get HEAD SHA Hash Using “$ git rev-parse” Command?
- How to Get Remote Branch SHA Hash Using “$ git rev-parse” Command?
- How to Get Current Working Branch Using “$ git rev-parse” Command?
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:
To get the SHA hash of HEAD’s current position, execute the “git rev-parse” command:
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:
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:
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:
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:
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.