Git
Commit \`Hash\`
Version Control
Git Commands
Software Development

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.

Browse interview questions

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.

bash
git show <commit-hash>

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.

bash
git cat-file -t <commit-hash>

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.

bash
git branch --contains <commit-hash>
git tag --contains <commit-hash>

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.

bash
git fetch --all --prune
git show <commit-hash>

Teams often share hashes from branches you have not fetched yet.

If you only have partial information, search by message, author, or patch text.

bash
git log --all --grep "fix pagination"
git log --all --author "alex"
git log --all -S "criticalFunctionName"

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.

bash
git rev-parse <short-hash>

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.

bash
git rev-parse --is-shallow-repository
git fetch --unshallow

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.

bash
git log --graph --decorate --oneline --all --max-count=30
git reflog --date=iso

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.

bash
git checkout -b recovery <hash-from-reflog>

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 show for direct commit lookup and details.
  • Use cat-file for 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
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Browse interview questions

All Rights Reserved.