Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

git

git-hash

How do I get the hash of the current commit in Git?

like image 574
Sardaukar Avatar asked Jun 04 '09 08:06

Sardaukar


People also ask

How do I see current commit hash?

# 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.

How do I get git hash from github?

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.


3 Answers

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.

like image 95
Jakub Narębski Avatar answered Oct 20 '22 16:10

Jakub Narębski


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.

like image 38
outofculture Avatar answered Oct 20 '22 15:10

outofculture


Another one, using git log:

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

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

like image 43
Paul Pladijs Avatar answered Oct 20 '22 14:10

Paul Pladijs