How to retrieve the hash for the current commit in Git?

ID : 353

viewed : 31

Tags : gitgit-hashgit

Top 5 Answer for How to retrieve the hash for the current commit in Git?

vote vote

91

To turn arbitrary extended object reference into SHA-1, use simply git-rev-parse, for example

git rev-parse HEAD 

or

git rev-parse --verify HEAD 

You can also retrieve the short version like this

git rev-parse --short HEAD 

Sidenote: If you want to turn references (branches and tags) into SHA-1, there is git show-ref and git for-each-ref.

vote vote

80

If you only want the shortened commit hash:

git log --pretty=format:'%h' -n 1 

Furthermore, using %H is another way to get the long commit hash, and simply -1 can be used in place of -n 1.

vote vote

73

Another one, using git log:

git log -1 --format="%H" 

It's very similar to the of @outofculture though a bit shorter.

vote vote

63

To get the full SHA:

$ git rev-parse HEAD cbf1b9a1be984a9f61b79a05f23b19f66d533537 

To get the shortened version:

$ git rev-parse --short HEAD cbf1b9a 
vote vote

54

For completeness, since no-one has suggested it yet. .git/refs/heads/master is a file that contains only one line: the hash of the latest commit on master. So you could just read it from there.

Or, as as command:

cat .git/refs/heads/master 

Update:

Note that git now supports storing some head refs in the pack-ref file instead of as a file in the /refs/heads/ folder. https://www.kernel.org/pub/software/scm/git/docs/git-pack-refs.html

Top 3 video Explaining How to retrieve the hash for the current commit in Git?

Related QUESTION?