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

To get the shortened commit hash, use the %h format specifier:

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

%H represents the long commit hash. Also, -1 can be used directly in place of -n 1.


Another one, using git log:

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

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


To get the full SHA:

$ git rev-parse HEAD
cbf1b9a1be984a9f61b79a05f23b19f66d533537

To get the shortened version:

$ git rev-parse --short HEAD
cbf1b9a

To turn any extended object reference into a hash, use git-rev-parse:

git rev-parse HEAD

or

git rev-parse --verify HEAD

To retrieve the short hash:

git rev-parse --short HEAD

To turn references (e.g. branches and tags) into hashes, use git show-ref and git for-each-ref.