Git command to display HEAD commit id?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
If you only need the commit id for the current HEAD, Git already exposes it directly. The shortest and most common command is git rev-parse HEAD, which prints the full hash of the commit that HEAD points to right now.
The Direct Command
HEAD is Git’s name for your current checked-out commit. In the normal case it points to the tip of the current branch, but it can also point directly at a specific commit in a detached state. Either way, this command prints the full commit id:
Example output:
This is usually the best answer because it is explicit, script-friendly, and returns only the hash.
If you want the abbreviated form that people often paste into tickets or chat, use:
That produces something like:
Other Valid Ways to Print the Same Commit
Git has several commands that can show the same information. They are useful when you already need commit metadata or when you are formatting output for a script.
Using git log:
Using git show:
Those commands also print the current commit hash. The difference is intent:
- '
git rev-parse HEADresolves a revision name to an object id.' - '
git log -1 --format=%Hasks Git for the latest commit and formats the result.' - '
git show -s --format=%H HEADshows one commit without the patch.'
For shell scripts, git rev-parse HEAD is usually the clearest option because it avoids extra formatting concerns.
Using the Commit Id in Scripts
The HEAD commit id is often used in build metadata, release artifacts, or debugging output. A simple shell example:
A Python script can capture it the same way:
This is helpful when you want application logs or generated files to record exactly what source revision produced them.
Related Revision Names
It helps to understand a few nearby names while working with HEAD.
HEAD means the current commit. HEAD~1 means the parent of the current commit. HEAD~2 means two commits back on the first-parent chain. If you only want the current commit id, do not add a suffix by accident.
You can also ask Git what branch HEAD is attached to:
That command is separate from the commit id. It prints the branch name when one exists, and it prints nothing useful in a detached HEAD state. The commit id still works in both cases.
Common Pitfalls
One common mistake is using git log without formatting and then trying to parse human-readable output in a script. Prefer git rev-parse HEAD or a formatted git show or git log command.
Another issue is running the command outside a Git repository. In that case Git reports that it is not inside a work tree, and there is no HEAD to resolve.
Some developers also confuse the current commit id with the working tree state. Uncommitted changes do not change HEAD. If you edit files without committing them, git rev-parse HEAD still prints the last committed revision.
Finally, remember that a short hash is only convenient, not guaranteed unique forever. For logs and automation, the full hash is safer.
Summary
- Use
git rev-parse HEADto print the full current commit id. - Use
git rev-parse --short HEADfor a shorter human-friendly hash. - '
git log -1 --format=%Handgit show -s --format=%H HEADare valid alternatives.' - Uncommitted changes do not affect
HEAD. - In scripts, prefer commands that output only the hash and nothing else.

