Git

How to Get the Short Git Version Hash

Git commit hash is useful in tagging releases and viewing the code’s state at a particular point in time. Commit id contains the 40 digits long SHA-hash, which can be abbreviated up to the shortest 7-digit version and can be settled by default.

Different commands are used to get the shortest version of the Git commits hashes, such as “git log” and “git rev-parse” commands. The “git log” command can be utilized along with the multiple options for desired output.

The outcomes of this post are:

How to Find the Short Git SHA-Hash of Current HEAD Position?

Use the “cd” command along with the Git root directory path and redirect to it:

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

To get the short SHA-hash of the commit where HEAD is pointing, run the following command:

$ git rev-parse --short HEAD

As you can see, the above-stated command just displays the HEAD pointer short commit SHA-hash:

How to Find the Short Git SHA-Hash of Desired Number of Commit?

To get the short SHA-hash of the particular number of commits, use the “git log” command:

$ git log -3 --pretty=format:%h

Here, the “-3” is our specified range of commits which we need to show the short version of Git SHA-hash, the “–pretty=format” will print the commits output in the specified format, and the “:%h” indicates the short version of commit hash:

How to Find the Short Git SHA-Hash Along With Commit Message and Branch Detail?

To get the short version of Git commits SHA-hash with the commit message and branch details, execute the “git log” command with the “–oneline” flag to display details in one line and desired number range:

$ git log --oneline -3

As you can see, the below-given output shows the short hash version of a specified number of commits, their respective commit messages, and pointing branches:

How to Find the Short Git SHA-Hash With Complete Commit Details?

Sometimes, users want to get the SHA-hash along with the details of the Git commits. For this purpose, utilize the below-stated command:

$ git log -3 --abbrev-commit

In the above-described command, the “–abbrev-commit” option is used for displaying the short commit hash:

How to Find the Short Git SHA-Hash With Commit Date and Time?

If developers want to view the short version of the hash with their committed date and time, then execute the following command:

$ git log --pretty="%h %cD %cn %s" -3

Here:

  • –pretty=” will print the commit in the specified format.
  • %h” indicates the SHA-hash.
  • %cD” will show the committed date.
  • %s” indicates the subject.
  • -3” is a specified range that displays the number of commit hashes:

That’s all! We have provided several methods of getting a short Git version of the commit hash.

Conclusion

There are different commands that are used for getting the short version of the commit SHA-hash, such as “git log”, and “git rev-parse” commands, and many more. The “git log” command can be utilized along with the multiple options for desired output. This post described multiple ways of getting a short Git version of the commit hash.

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.