2 Methods to Get Current Commit Hash in Git

2 Methods to Get Current Commit Hash in Git

A commit hash is a unique identifier assigned to each commit in a Git repository. It can be used to reference a specific revision when reviewing changes, comparing versions, or troubleshooting issues. This tutorial provides 2 methods how to get the current commit hash in Git.

Method 1 - git rev-parse

The git rev-parse command can retrieve the identifier of the current commit, which is the commit referenced by HEAD. Run the following command to display the complete commit hash:

git rev-parse HEAD

Example output:

9a06a8cf2d0f8e13dc5dac705c770fcbe68d5734

To display a shortened version of the hash, use the --short option:

git rev-parse --short HEAD

Example output:

9a06a8c

Method 2 - git log

The git log command can also retrieve the hash of the most recent commit. Adding -1 limits the output to a single commit, and the --format option specifies which part of the commit information is displayed.

To print the complete commit hash, execute:

git log -1 --format=%H

For the shortened hash, use the lowercase %h format placeholder:

git log -1 --format=%h

Leave a Comment

Cancel reply

Your email address will not be published.