Categories
git git-hash

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

2406

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

1

  • Use git log to retrieve recent commits, that will show full commit hash

    Mar 15 at 14:23

3519

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.

20

  • 97

    --verify implies that: The parameter given must be usable as a single, valid object name. Otherwise barf and abort.

    Jul 24, 2011 at 17:50

  • 686

    git rev-parse --short HEAD returns the short version of the hash, just in case anyone was wondering.

    Oct 25, 2012 at 21:28

  • 63

    Adding to what Thane said, you can also add a specific length to --short, such as --short=12, to get a specific number of digits from the hash.

    Feb 21, 2014 at 17:18

  • 42

    @TysonPhalp: --short=N is about minimal number of digits; git uses larger number of digits if shortened one would be undistinguishable from shortened other commit. Try e.g. git rev-parse --short=2 HEAD or git log --oneline --abbrev=2.

    Feb 21, 2014 at 18:08

  • 44

    Adding to what Thane, Tyson, and Jakub said, you can print the full hash, but highlight the hexits necessary to identify the commit blue with git rev-parse HEAD | GREP_COLORS='ms=34;1' grep $(git rev-parse --short=0 HEAD)

    – Zaz

    Aug 5, 2014 at 16:44

512

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.

10

  • 117

    Or, it seems, adding –short to the rev-parse command above seems to work.

    Sep 30, 2011 at 23:39

  • 20

    I think git log is porcelain and git rev-parse is plumbing.

    Jan 29, 2016 at 10:40

  • 5

    This is a bad/ incorrect way of doing it because this method will give you the wrong hash if you have a detached head. For example if the current commit is 12ab34… and the previous commit was 33aa44… then if i do ‘git checkout 33aa44’ and then I run your command I will still be getting back 12ab34… despite my head actually pointing to 33aa44…

    Jul 17, 2017 at 0:03


  • 5

    @theQuestionMan I don’t experience the behavior you describe; git checkout 33aa44; git log -n 1 gives me 33aa44. What version of git are you using?

    Jul 19, 2017 at 17:32

  • 9

    @AmedeeVanGasse, ah! I HAD NO IDEA this is a toilet analogy! I’ve been seeing porcelain in the git man pages for years, but had NO idea it was referring to a toilet! The porcelain is the toilet, and it’s “closer to the user” (who figuratively sits on this toilet) than the plumbing, which is lower-level and farther from the user–ie: below the “porcelain”! Mind blown.

    Feb 21, 2021 at 7:26


185

Another one, using git log:

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

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

7

  • 1

    And the result is not single-quoted.

    – crokusek

    Feb 28, 2017 at 23:11

  • 10

    This is the correct answer, since it works even if you checkout a specific commit instead of HEAD.

    – Parsa

    Feb 22, 2019 at 19:16

  • 3

    @Parsa: when checking out a specific commit HEAD points to this commit rather than a named branche know as detached head.

    Jan 28, 2020 at 20:46

  • 1

    From the command line, to avoid pager: git --no-pager log -1 --format="%H"

    – ederag

    May 9, 2021 at 20:27

  • @Parsa Your (mistakenly upvoted?) comment is misleading as the accepted answer of jakub-narębski ‘git rev-parse HEAD’ works after checking out somewhere else.

    – spawn

    May 12 at 14:54