Get the short Git commit hash
Codemia Knowledge Hub ·
Run git rev-parse --short HEAD to get the short hash of the current commit. Git abbreviates commit ids safely as long as the shortened prefix is still unique in the repository, which makes short hashes handy for logs, UI display, release metadata and build output.
Short hash for the current commit
This prints a shortened hash for HEAD. Git picks a length that is unique enough for the repository unless you specify one.
Fixed short hash length
If a build system or UI needs a specific length, pass it directly:
This suits version strings, but very short lengths can become ambiguous in larger repositories.
Short hash with git log
git log formatting also gives abbreviated hashes:
%h is the abbreviated commit hash. This is convenient when you are already formatting other commit metadata, for example:
That prints the short hash and the commit subject together, which is handy in release scripts or shell prompts.
Which command to use in scripts
For scripting, git rev-parse --short HEAD is usually clearer because it is explicitly about resolving a reference. Use git log when you also want the message, author or date in the same output. Keeping that distinction makes shell scripts more readable.
How short is safe
A short hash is only useful while it stays unique. Git handles this reasonably, but in a very large repository, or with a tiny hardcoded abbreviation length, collisions become possible. In practice:
- let Git choose the abbreviation when possible
- use a slightly longer fixed length for external systems
- avoid assuming 7 characters is always enough forever
If the hash is embedded in build artifacts or shown in a dashboard, choose a length that stays readable while leaving room for the repository to grow. Short hashes are ideal for human-facing output; for machine identity, the full hash is the safer long-term reference.
Mistakes with short hashes
- Hardcoding a very short hash length in a large repository.
- Using
git logformatting in scripts when a simpler reference-resolution command would be clearer. - Assuming a short hash is guaranteed unique across all clones and all future history.
- Forgetting that detached-head or tag-based contexts still resolve through
HEADdifferently depending on checkout state. - Mixing user-facing display needs with machine-stable identifier requirements.
Related reading
- Getting current branch and commit hash in GitHub action
- getting fatal not a git repository '.' when using post-update hook to execute 'git pull' on another repo
- Getting Git to work with a proxy server - fails with Request timed out
- Getting the difference between two repositories
- git-checkout older revision of a file under a new name
- Git-svn create push a new branch/tag?
- Git - Difference Between 'assume-unchanged' and 'skip-worktree
- Git - fatal Unable to create '/path/my_project/.git/index.lock' File exists
.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 courseNo course covers this one yet
Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.
Build my courseInterview Questions practice on Codemia
Over 11,000 interview practice questions for 300+ companies, searchable by company and role.