git rev-parse Command: Resolve Refs in CI
git rev-parse turns refs, branch names, and shorthand into the full object IDs Git uses internally.
rev-parse is the workhorse for scripts that need a commit SHA, the current branch name, or the repo root. It powers most CI build-tagging and image-labeling steps.
Common flags
HEAD- resolve the current commit to its full 40-character SHA--short- print an abbreviated (default 7-char) SHA--abbrev-ref HEAD- print the current branch name (or "HEAD" if detached)--show-toplevel- print the absolute path to the repository root--verify <ref>- verify a ref exists and print its SHA, failing if not--is-inside-work-tree- print true when inside a working tree
Example
shell
# Build a short SHA and branch for image tags
SHA="$(git rev-parse --short HEAD)"
BRANCH="$(git rev-parse --abbrev-ref HEAD)"
echo "Building ${BRANCH}@${SHA}"In CI
rev-parse --short HEAD is the canonical way to derive a short commit ID for Docker tags and artifact names. Note that in detached-HEAD checkouts (common in CI) --abbrev-ref HEAD returns "HEAD"; use the platform branch variable when you need the real branch name.
Key takeaways
- git rev-parse --short HEAD produces a stable short SHA for tagging artifacts.
- --abbrev-ref HEAD returns "HEAD" in detached checkouts, so prefer CI env vars for branch names.
- --show-toplevel reliably locates the repo root inside scripts.
Related guides
git log Command: Inspect History in CIgit log shows commit history. Reference for --oneline, -n, and --format to extract commit messages and author…
git describe Command: Version Strings in CIgit describe builds a human-readable version from tags. Reference for --tags and --always to derive build ver…
git symbolic-ref Command in CIgit symbolic-ref reads and sets symbolic refs like HEAD. Reference for finding a remote default branch reliab…
git rev-list Command: Count Commits in CIgit rev-list lists commit objects in reverse chronological order. Reference for --count to derive monotonic b…