Using Git Rev-Parse to Print Commit IDs

In this short blog, let’s explore a small but very useful Git command: git rev-parse.
It is often used in scripts and automation to print commit IDs from references like branches or HEAD.


Example with Branch

First, check your commit history:

git log --oneline

Output:

c3e4f3c Third commit
9a7b1d2 Second commit
4b1c2a1 First commit

Now, if you want the commit ID of the branch main:

git rev-parse main

Output:

c3e4f3c7bdbe9d6b...

This is the hash of the commit currently pointed to by main.


Example with HEAD

To print the commit ID of the commit where HEAD is pointing:

git rev-parse HEAD

Output:

c3e4f3c7bdbe9d6b...

This is especially useful when you need the current commit hash for logging, debugging, or CI/CD pipelines.


Why Is This Useful?

  • Scripting and automation: Get commit IDs dynamically.
  • Debugging: Quickly check where branches or tags point.
  • Builds and deployments: Pass commit hashes to tools or logs.

Summary

  • git rev-parse <ref> prints the commit ID for the given reference.
  • Works with main, HEAD, or any branch/tag.
  • Commonly used in automation, scripts, and pipelines.

Keep Learning 🚀

👉 Subscribe to Learning Ocean – Subscribers get coupon codes for my courses, early access to blogs and courses, and even exclusive YouTube videos.

👉 My YouTube Channel – More videos, more fun, and lots of learning!

👉 📺 Watch this topic in video form

Stay curious, keep coding, and let’s make learning fun together! 🎉