Get the short Git version hash

Git

Git Problem Overview


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

Git Solutions


Solution 1 - Git

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.

Solution 2 - Git

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

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

Solution 3 - Git

git log -1 --abbrev-commit

will also do it.

git log --abbrev-commit

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

Solution 4 - Git

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

Solution 5 - Git

A really simple way is to:

git describe --always

Solution 6 - Git

Branch with short hash and last comment:

git branch -v

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

Solution 7 - Git

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

Solution 8 - Git

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAttila O.View Question on Stackoverflow
Solution 1 - GitMark LongairView Answer on Stackoverflow
Solution 2 - GitKarl BielefeldtView Answer on Stackoverflow
Solution 3 - GitSanjeevView Answer on Stackoverflow
Solution 4 - GitThe Gilbert Arenas DaggerView Answer on Stackoverflow
Solution 5 - GitSteven ShawView Answer on Stackoverflow
Solution 6 - GitFabriceView Answer on Stackoverflow
Solution 7 - GitDown the StreamView Answer on Stackoverflow
Solution 8 - GitvelocityView Answer on Stackoverflow