Git
Version Control
Short `Hash`
Git Command
Code Management

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

bash
git rev-parse --short HEAD

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:

bash
git rev-parse --short=8 HEAD

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:

bash
git log -1 --pretty=format:%h

%h is the abbreviated commit hash. This is convenient when you are already formatting other commit metadata, for example:

bash
git log -1 --pretty=format:'%h %s'

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:

  1. let Git choose the abbreviation when possible
  2. use a slightly longer fixed length for external systems
  3. 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 log formatting 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 HEAD differently depending on checkout state.
  • Mixing user-facing display needs with machine-stable identifier requirements.

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
Basis Lab
No 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 course
Interview Questions practice on Codemia

Over 11,000 interview practice questions for 300+ companies, searchable by company and role.

Browse interview questions