How do I get the hash of the current commit in Git?
# open the git config editor $ git config --global --edit # in the alias section, add ... [alias] lastcommit = rev-parse HEAD ... From here on, use git lastcommit to show the last commit's hash.
To search for a hash, just enter at least the first 7 characters in the search box. Then on the results page, click the "Commits" tab to see matching commits (but only on the default branch, usually master ), or the "Issues" tab to see pull requests containing the commit.
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
.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With