Skip to content
Latchkey

git rev-parse: Usage, Options & Common CI Errors

git rev-parse turns refs into SHAs and answers "where am I" questions scripts need.

rev-parse is plumbing every CI script reaches for: get the commit SHA, the branch name, or the repo root.

What it does

git rev-parse parses and resolves revision and path arguments - turning HEAD or a branch into a full SHA, reporting the current branch, the repo top level, and more.

Common usage

Terminal
git rev-parse HEAD                # full commit SHA
git rev-parse --short HEAD        # short SHA
git rev-parse --abbrev-ref HEAD   # current branch name
git rev-parse --show-toplevel     # repo root directory
git rev-parse --is-inside-work-tree

Options

FlagWhat it does
--short[=n]Abbreviated SHA
--abbrev-ref <ref>Symbolic (branch) name of a ref
--show-toplevelAbsolute path to the repo root
--verify <ref>Validate and resolve a single ref
--is-inside-work-treePrint true/false

Common errors in CI

fatal: ambiguous argument 'HEAD': unknown revision or path not in the working tree - usually run outside a repo or a fresh repo with no commits. In detached HEAD, --abbrev-ref HEAD returns "HEAD" rather than a branch name; read the branch from the CI context instead.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →