How to find a commit by its hash?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Finding a Git commit by hash is straightforward once you know whether the commit exists locally, on another branch, or only on a remote. The hash can be full or abbreviated, but full hashes are safer in scripts and audits. A small set of Git commands covers most lookup workflows.
Core Sections
Show Commit Details Directly
If the commit exists locally, git show is usually enough.
This displays commit metadata, message, and patch. If hash is ambiguous, Git will ask for a longer identifier.
Verify Existence Without Full Patch
When you only need to check if a hash exists, use cat-file.
If the object exists and is a commit, output is commit. This is useful in automation scripts.
Search Across All Local Branches
A commit may exist but not be on your current branch. Use branch containment queries.
This tells you where the commit is reachable, which helps during release backtracking.
Fetch Remote References if Missing
If lookup fails locally, fetch from remote before concluding the hash is unknown.
Teams often share hashes from branches you have not fetched yet.
Find Commits by Partial or Related Data
If you only have partial information, search by message, author, or patch text.
These commands help recover commit context when exact hash is unavailable.
Use Full Hashes in Critical Workflows
Abbreviated hashes can become ambiguous as repositories grow. For CI pipelines, release notes, and security audits, store full 40-character hashes.
This resolves a short hash to full form when unambiguous.
Troubleshooting Missing Hashes
If a hash still cannot be found, possibilities include force-pushed-away commits, shallow clone history, or hash copied incorrectly. Check whether your clone is shallow and deepen history if needed.
In forensic cases, git fsck --lost-found can help locate dangling objects.
Investigate Context with Graph and Reflog Tools
After finding a commit, you often need to understand why it is missing from a branch or release line. Graph views and reflog data provide context quickly.
If a commit disappeared after a force push, reflog may still show previous branch tips locally. You can create a recovery branch from relevant reflog entries before garbage collection removes unreachable objects.
In incident response, always capture recovered hashes in ticket notes with exact timestamps and branch names. Clear traceability matters when multiple people investigate history divergence.
For compliance workflows, capture both commit hash and verified branch tip in release records. This ensures traceability even after branch rewrites.
Consistent repository hygiene and fetch policies reduce false missing-hash investigations.
When multiple remotes exist, include remote names in investigation notes so teammates can reproduce searches without ambiguity.
Consistent communication around hashes prevents release-traceability gaps.
These habits reduce debugging time during release incidents.
Common Pitfalls
- Assuming a missing hash means deletion before fetching all remote refs.
- Using very short hashes that collide in large repositories.
- Searching only current branch instead of all refs.
- Forgetting shallow-clone limits in CI environments.
- Copying hashes with hidden whitespace characters from chat tools.
Summary
- Use
git showfor direct commit lookup and details. - Use
cat-filefor existence checks in scripts. - Search branch and tag containment for context.
- Fetch remote history before concluding a hash is absent.
- Prefer full hashes for long-term traceability.
Related reading
- How to find a deleted file in the project commit history?
- How to find a Github file 's SHA blob
- How to find origin of a branch in git?
- How to find the Git commit that introduced a string in any branch?
- How to find the nearest parent of a Git branch
- How to fix committing to the wrong Git branch?
- How to fix committing to the wrong Git branch?
- How to fix corrupted interactive rebase?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.