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.

Using this in CI

CI checkouts are shallow and detached by default, which changes the answer this command gives you. Commands that read history, branch names, or tags need the checkout configured for it.

.github/workflows/ci.yml
- uses: actions/checkout@v4
  with:
    fetch-depth: 0   # history, tags, and git describe all need this

- run: |
    git rev-parse --is-shallow-repository   # expect false
    git rev-parse --abbrev-ref HEAD          # prints HEAD when detached

Frequently asked questions

git rev-parse: Usage, Options & Common CI Errors?
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 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

References

Run this faster and cheaper on Latchkey managed runners - self-healing included. Start free → 30-day trial · No credit card