Get the short Git version hash
Asked Answered
S

9

615

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

Shad answered 17/4, 2011 at 15:29 Comment(5)
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.Kloman
@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.Shad
Huh. When I run git describe --tags I get the message, "fatal: No names found, cannot describe anything.".Urinary
@QuinnComendant You probably need to tag something first for --tags to work. Try creating a tag first; e.g. git tag 1.0.0.Shad
Possible duplicate of git get short hash from regular hashHeliotherapy
G
1114

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.

Georgenegeorges answered 17/4, 2011 at 15:34 Comment(7)
you can do the reverse and get the long commit hash from the short commit hash by doing the following git rev-parse HEADRelique
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 97dd2aeHausa
It just works with references. You can use HEAD, tag names, branch names or plain hashes.Fley
Warning, this returns a 7 character commit hash (by default) while many places like gitlab use 8 characters!Fabyola
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 SourcePony
At Ubuntu 20.04 git rev-parse --short=7 ... returns 8 charecters for me. even if I put --short=6 or less - it returns 8!!Tenishatenn
Also important to know is that the number you provide to --short=n is just a minimum. It may return a longer string in order to guarantee that hash points to a unique commit. From the docs: "[it] shortens the object name to a unique prefix with at least length characters".Multidisciplinary
M
167

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:".

Monospermous answered 17/4, 2011 at 15:40 Comment(0)
U
123
git log -1 --abbrev-commit

will also do it.

git log --abbrev-commit

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

Upper answered 20/5, 2016 at 17:42 Comment(2)
Also works with git log --pretty=oneline, which unlike --oneline, otherwise prints full size hashes.Elaterium
this seems a lot cleaner than pretty format.Treasonous
B
74

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
Baking answered 16/5, 2017 at 17:14 Comment(2)
--oneline is the best optionSpoofery
@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 logVinegarish
C
42

A really simple way is to:

git describe --always
Carpio answered 17/2, 2015 at 5:13 Comment(4)
ha, sweet, that addresses the cases where git describe will fail otherwise (because describe expects a tag somewhere in history) thxTile
Not good if you strictly want the short hash - since this can return an annotated tag is there is one.Disprove
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]Gader
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!Carpio
A
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
...
Assiduity answered 7/3, 2018 at 15:59 Comment(2)
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.Medlar
This is what I am looking for. Vote for my question too!Meehan
C
21

Branch with short hash and last comment:

git branch -v

  develop      717c2f9 [ahead 42] blabla
* master       2722bbe [ahead 1] bla
Cristiecristin answered 25/6, 2014 at 9:42 Comment(0)
V
8

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

Vinegarish answered 13/2, 2020 at 15:12 Comment(0)
T
0

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) }'
Tew answered 27/8, 2023 at 15:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.