How to get the latest git commit SHA-1 in a repository?

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

Answered by Eric Z Ma.

Eric Z Ma

Eric is a father and systems guy. Eric is interested in building high-performance and scalable distributed systems and related technologies. The views or opinions expressed here are solely Eric's own and do not necessarily represent those of any third parties.

Leave a Reply

Your email address will not be published. Required fields are marked *