637

Is there a cleaner way to get the short version hash of HEAD from Git?

I want to see the same output as I get from:

 git log -n 1 | head -n 1 | sed -e 's/^commit //' | head -c 8

I originally used the above command to generate a version string, but this is even better:

git describe --tags

It will output strings like 0.1.12 (tagged commit) or 0.1.11-5-g0c85fbc (five commits after the tag).

5
  • 3
    Since you seem to be good at manipulating data with pipes and whatnot, you should know about git aliases. In this case, there is a command for what you want (see answers) but eventually you will find something where there is not, and aliases are great for that.
    – Tyler
    Commented Apr 19, 2011 at 4:02
  • @MatrixFrog thanks for the tip! I already did have some simple git aliases, but I didn't know just how powerful they can be until now. I especially like the graphviz display.
    – Attila O.
    Commented Apr 19, 2011 at 19:39
  • 1
    Huh. When I run git describe --tags I get the message, "fatal: No names found, cannot describe anything.". Commented Jan 28, 2017 at 13:02
  • @QuinnComendant You probably need to tag something first for --tags to work. Try creating a tag first; e.g. git tag 1.0.0.
    – Attila O.
    Commented Jun 29, 2017 at 8:19
  • Possible duplicate of git get short hash from regular hash Commented May 9, 2018 at 11:43

9 Answers 9

1149

Try this:

git rev-parse --short HEAD

The command git rev-parse can do a remarkable number of different things, so you'd need to go through the documentation very carefully to spot that though.

7
  • 6
    you can do the reverse and get the long commit hash from the short commit hash by doing the following git rev-parse HEAD
    – Andrew
    Commented Jan 12, 2015 at 17:21
  • 21
    The command also works with long rev IDs that are copy-pasted from the other sources like git log, eg git rev-parse --short 97dd2ae065771908ee9ae0fa08ccdb58b5a6b18f returns 97dd2ae
    – chiborg
    Commented Jan 15, 2016 at 14:55
  • 5
    It just works with references. You can use HEAD, tag names, branch names or plain hashes.
    – d12frosted
    Commented Apr 4, 2016 at 16:28
  • 7
    Warning, this returns a 7 character commit hash (by default) while many places like gitlab use 8 characters!
    – masterxilo
    Commented Nov 13, 2019 at 19:37
  • 47
    You can use git rev-parse --short=8 HEAD to get the 8 character length that is used by GitLab. You can also set core.abbrev to 8 for a specific git repo with a command like git config core.abbrev 8 Source
    – n8felton
    Commented Nov 25, 2019 at 13:29
175

You can do just about any format you want with --pretty=format:

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

The meaning of %h, from man git log, is:

%h
abbreviated commit hash

To see other format options, see man git log and search for the section that begins with the phrase "Placeholders that expand to information extracted from the commit:".

0
127
git log -1 --abbrev-commit

will also do it.

git log --abbrev-commit

will list the log entries with abbreviated SHA-1 checksum.

2
  • 3
    Also works with git log --pretty=oneline, which unlike --oneline, otherwise prints full size hashes.
    – sdaau
    Commented May 16, 2020 at 16:54
  • this seems a lot cleaner than pretty format. Commented Mar 5 at 22:33
77

A simple way to see the Git commit short version and the Git commit message is:

git log --oneline

Note that this is shorthand for

git log --pretty=oneline --abbrev-commit
2
  • 2
    --oneline is the best option Commented Jul 23, 2019 at 13:57
  • 1
    @JuanIgnacioBarisich the best option depends on how much information you need to view. In case one needs more information like author or date then git log --abbrev-commit would be a better option. also log --pretty might be a better option to choose which information to log
    – velocity
    Commented Feb 13, 2020 at 15:05
43

A really simple way is to:

git describe --always
4
  • 1
    ha, sweet, that addresses the cases where git describe will fail otherwise (because describe expects a tag somewhere in history) thx
    – keen
    Commented May 18, 2016 at 23:21
  • 14
    Not good if you strictly want the short hash - since this can return an annotated tag is there is one.
    – Zitrax
    Commented Jun 9, 2016 at 12:15
  • In some cases git describe --long could help. From the docs: "Always output the long format (the tag, the number of commits and the abbreviated commit name) even when it matches a tag." [my emphasis]
    – djvg
    Commented Apr 23, 2020 at 19:56
  • Using --long is better but sometimes you get a short hash and sometimes 3 items separated by hyphens. These days, I use the accepted answer. Back in the day, I didn't know about annotated tags — perhaps they didn't even exist! Commented Apr 23, 2020 at 23:21
23

I have Git version 2.7.4 with the following settings:

git config --global log.abbrevcommit yes
git config --global core.abbrev 8

Now when I do:

git log --pretty=oneline

I get an abbreviated commit id of eight digits:

ed054a38 add project based .gitignore
30a3fa4c add ez version
0a6e9015 add logic for shifting days
af4ab954 add n days ago
...
2
  • 1
    While this code may answer the question, providing additional context regarding how and why it solves the problem would improve the answer's long-term value. Commented Mar 7, 2018 at 18:07
  • This is what I am looking for. Vote for my question too!
    – Tung
    Commented Oct 31, 2023 at 15:46
21

Branch with short hash and last comment:

git branch -v

  develop      717c2f9 [ahead 42] blabla
* master       2722bbe [ahead 1] bla
0
9

what about this :

git log --pretty="%h %cD %cn %s"  

it shows someting like :

674cd0d Wed, 20 Nov 2019 12:15:38 +0000 Bob commit message

see the pretty format documentation

0
1

Also, if you need to get short commit SHA from the remote repository you can use the next command:

git ls-remote https://some.domain/some-remote-repo.git HEAD | awk '{ print substr($1,1,8) }'

Not the answer you're looking for? Browse other questions tagged or ask your own question.