How to get the latest git commit SHA-1 in a repository?
Posted on In QAHow to get the latest git commit SHA-1 id in a repository?
And how to get the first 8 digits of the SHA-1?
Instead of the method introduced here, you may use
$ git rev-parse HEAD
to get the commit SHA-1 hash ID.
If you want to get the first 8 digits only, use
$ git rev-parse HEAD | cut -c 1-8
Here, cut -c 1-8
gives you bytes 1 to 8.
BTW: if you have tags tagging the versions, you may use
$ git describe --tags --long
to get a better string for IDs like
v2.0-40-gdc25d60
Here, ‘v2.0′ is the latest tag. ’40’ is the number of commits after the ‘v2.0’ tag and ‘gdc25d60’ is the first 8 digits of the commit sha-1 hash ID.
Rather than cutting it, allow `git` to shorten the SHA on your behalf:
git rev-parse –short HEAD
From the docs:
–short[=length]
Same as –verify but shortens the object name to a unique prefix with at least length characters. The minimum length is 4, the default is the effective value of the core.abbrev configuration variable (see git-config[1]).