Get the current Git commit hash

Resolve HEAD to a full or abbreviated commit ID, handle repositories without a first commit, and label builds without confusing the ID with uncommitted changes.

Best practices
Illustration of Git branches and commit history

Use git rev-parse HEAD to print the object ID of the current commit. For an abbreviated ID, use git rev-parse --short HEAD.

Full and abbreviated IDs

git rev-parse HEAD
git rev-parse --short HEAD
git log -1 --format=%H
git log -1 --format=%h

A full ID has 40 hexadecimal characters in a SHA-1 repository and 64 in a SHA-256 repository. Do not hard-code 40 characters into a script that must support both object formats.

An abbreviation is not always seven characters. Its length depends on the abbreviation setting and uniqueness within the local object database. git rev-parse --short=7 HEAD requests a minimum length of seven; Git can extend it to distinguish the object. Use full IDs for durable records or exchanges across repositories, and retain the repository identity too.

Use a verified ID in a script

When a script needs a commit, validate that HEAD resolves to one before using its output:

if commit_id=$(git rev-parse --verify --quiet 'HEAD^{commit}'); then
    printf '%s\n' "$commit_id"
else
    printf '%s\n' 'No current commit could be resolved.' >&2
    exit 1
fi

The failure can mean an unborn branch with no first commit, an invalid repository context, or another resolution problem. Without --verify, simply suppressing stderr can still leave unwanted text on stdout in some failure cases.

For a build label, use the verified value:

docker build -t "myapp:$commit_id" .
printf '{"commit":"%s"}\n' "$commit_id" > version.json

Run these after successful verification; version.json is an example output path. A commit ID identifies committed content. It does not describe uncommitted edits, build arguments, dependencies, or other files included in a Docker build context. Use a clean, controlled source checkout when the label needs to identify the build input accurately. The generated metadata file itself also needs deliberate handling in that workflow.

Resolve related revisions

Revision Meaning
HEAD Current commit
HEAD^ First parent of the current commit, when one exists
HEAD~3 Follow the first-parent chain three times
@{u} Upstream of the current branch, if configured
origin/main A local remote-tracking ref from a fetch

For example, git rev-parse --verify 'HEAD~3^{commit}' resolves that ancestor when it exists. Quote revision expressions in scripts so the shell does not reinterpret them. origin/main can be stale until fetched, and a root commit has no parent.

Does this work in detached HEAD?

Yes. Detached HEAD points directly to a commit instead of through a branch name; its commit ID can still be resolved. In a repository without any commit, resolution fails. An abbreviated ID is unique only in the relevant local object set at the time it is generated; that is another reason to store the full ID for long-lived records.

References

Get a tailored plan

Contact us

Join the community

Scan with WeChat
to join the community

Try Guance

Start online and pay only for what you use.

Get started

Choose a Guance plan

Code hosting