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-treeOptions
| Flag | What it does |
|---|---|
| --short[=n] | Abbreviated SHA |
| --abbrev-ref <ref> | Symbolic (branch) name of a ref |
| --show-toplevel | Absolute path to the repo root |
| --verify <ref> | Validate and resolve a single ref |
| --is-inside-work-tree | Print 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
git show: Usage, Options & Common CI Errorsgit show displays a commit, tag, or object with its diff. Reference for --stat, --name-only, blob viewing, an…
git symbolic-ref: Usage, Options & Common CI Errorsgit symbolic-ref reads and writes symbolic refs like HEAD, used to find or set the default branch. Reference…
git cat-file: Usage, Options & Common CI Errorsgit cat-file inspects Git objects - their type, size, and content. Reference for -t, -s, -p, --batch, and the…
git describe: Usage, Options & Common CI Errorsgit describe produces a readable name from the nearest tag, ideal for build versions. Reference for --tags, -…