Get the short Git version hash

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

8 s
8

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.

Leave a Comment