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

Posted on

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.

Eric Ma

Eric is a 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.

One comment

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

Leave a Reply

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

Related Posts