How to get the latest commit’s hash in git?

I am looking for a better method for getting the latest commit’s hash in git.

I know git log -1 can show the info of the latest commit and from the info of the latest commit I can use grep and other tools to get the hash.

Is there better method supported from git?

Note: a better method is introduced in this QA.

You can use the --format option of git log:

git log -n1 --format=format:"%H"

Here, “%H” means “commit hash”.

For explanation and other options of the format string, please check git log manual.

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.

Leave a Reply

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