362

What command can I use to print out the commit id of HEAD?

This is what I'm doing by hand:

$ cat .git/HEAD
ref: refs/heads/v3.3
$ cat .git/refs/heads/v3.3
6050732e725c68b83c35c873ff8808dff1c406e1

But I need a script that can reliably pipe the output of some command to a text file such that the text file contains exactly the commit id of HEAD (nothing more or less, and not just a ref). Can anyone help?

Jon Seigel
  • 12,251
  • 8
  • 58
  • 92
Andrew Arnott
  • 80,040
  • 26
  • 132
  • 171
  • I have a similar question before. And it was well answered here: https://stackoverflow.com/questions/44994626/why-git-use-2-different-commands-to-show-head – smwikipedia Jul 09 '17 at 09:20

8 Answers8

639

Use the command:

git rev-parse HEAD

For the short version:

git rev-parse --short HEAD
Jorge Ferreira
  • 96,051
  • 25
  • 122
  • 132
Randal Schwartz
  • 39,428
  • 4
  • 43
  • 70
  • 50
    For anyone looking for the short version of the hash, `git rev-parse --short HEAD` will work. – mgarciaisaia Apr 13 '15 at 18:21
  • 6
    I can't believe that this answer (straight from the documentation) is still getting up-votes 13 years later. :) – Randal Schwartz Apr 23 '22 at 17:58
  • 2
    @RandalSchwartz Your answer helps now ==> you get an upvote now. – Yaakov Belch Oct 06 '22 at 20:00
  • 3
    Googling for an answer (and arriving here) is much faster than reading through pages and pages of documentation when you don't know exactly what you're looking for. – GreenGiant Nov 09 '22 at 15:58
  • The smallest payload is indeed a megabyte or so. Think of this as a "polyfill": a layer to give all browsers the equal abilities to render a carefully controlled canvas. Thus, it's unfair to "hello world" programs, but becomes only a tiny part of much larger apps. – Randal Schwartz Jun 04 '23 at 17:48
71
git log -1

for only commit id

git log | head -n 1 
cyb0k
  • 2,478
  • 23
  • 19
  • Thanks, it's easy – eC Droid May 15 '19 at 09:40
  • Just FYI: `git log | head -1` is as same as `git log | head -n 1` . – Milan May 26 '21 at 18:57
  • Although the OP has only asked for the commit ID of HEAD, `git log -1` is very useful. Because it also displays the commit message and other details which makes further analysis/comparison easy. Thank you! – Milan May 26 '21 at 19:00
21

Old thread, still for future reference...:) even following works

git show-ref --head

by default HEAD is filtered out. Be careful about following though ; plural "heads" with a 's' at the end. The following command shows branches under "refs/heads"

 git show-ref --heads
Abhijit Mazumder
  • 8,641
  • 7
  • 36
  • 44
  • 5
    using these commands in a (large) git repo, I get two lines from `git show-ref --heads` and 6290 lines from `git show-ref --head`. so if you want just a single hash, this gives maybe not the intended result. – Remigius Stalder Jun 15 '17 at 08:47
15

You can specify git log options to show only the last commit, -1, and a format that includes only the commit ID, like this:

git log -1 --format=%H

If you prefer the shortened commit ID:

git log -1 --format=%h

JotaBe
  • 38,030
  • 8
  • 98
  • 117
6

Play with Bash:

git show HEAD | sed -n 1p | cut -d " " -f 2
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ali Moreno
  • 69
  • 1
  • 2
1

git rev-parse --abbrev-ref HEAD

Avdhut Mankavale
  • 395
  • 3
  • 12
  • 1
    This does not show commit ID, but rather any names (like branches or tags) associated with the commit. This is not what the original user wanted. – Asfand Qazi Nov 08 '18 at 11:42
  • 1
    @AsfandQazi but it was exactly what I wanted to know, and my google search gave me this article at the top. – Axel Bregnsbo Feb 09 '21 at 09:28
1

You can use this command

$ git rev-list HEAD

You can also use the head Unix command to show the latest n HEAD commits like

$ git rev-list HEAD | head -n 2

Attila
  • 3,206
  • 2
  • 31
  • 44
medmik
  • 19
  • 3
0

You can use

git log -g branchname

to see git reflog information formatted like the git log output along with commit id.

Stuti Verma
  • 1,059
  • 13
  • 32