Git

How do I Get the Commit id of the Head of master in Git?

Commit id is the unique value generated whenever a new commit is added and used while merging commits or checking out files from different commits. More specifically, the head of the branch is a reference to the specific commit, and a unique pointer represents it. Developers use the commit id to track changes, revert to the previous version, resolve conflicts, etc.

This write-up will explain the methods of getting the commit id of the head of Master in Git.

How to Get/Retrieve the Commit id of the Head of “master” in Git?

To get/retrieve the commit id of the head of “master”, various Git commands are available, such as:

Note: First, navigate to the master branch in order to get its commit id.

Method 1: Get Commit of Head of “master” Utilizing “git rev-parse HEAD” Command

Execute the below-provided command to get the commit id of the head of the master branch:

$ git rev-parse HEAD

The given output displays the full SHA-hash value of the HEAD of the “master” branch:

Moreover, in order to get the short commit id, utilize the “–short” option with the previous command:

$ git rev-parse --short HEAD

Here, the “–short” option shows the first seven digits of the commit id:

Method 2: Get Commit of Head of “master” Utilizing “git show HEAD” Command

In order to get the detailed information of the HEAD including the commit id, run the following command:

$ git show HEAD

In the below-provided screenshot, the commit id of the HEAD, commit message, date, author, etc., can be seen:

Method 3: Get Commit of Head of “master” Utilizing “git log -1” Command

Type out the provided command to get the SHA-hash of the head of the master branch:

$ git log -1

Here, the highlighted part is the commit id of the head of the “master” branch:

We have explained the various methods to get the commit id of the head of Master in Git.

Conclusion

Different Git commands are available to get the commit id of the head of the master branch in Git, such as the “git rev-parse HEAD” command is used for getting the full commit id, and the “–short” option with the same command is utilized to get the short SHA-hash value. Moreover, to get detailed information about the HEAD, the “git show HEAD” and “git log -1” commands can be used. This write-up explained the methods of getting the commit id of the head of master in Git.

About the author

Laiba Younas

I have done bachelors in Computer Science. Being passionate about learning new technologies, I am interested in exploring different programming languages and sharing my experience with the world.