Skip to content
Tutorials Center
My learning

Topics

Git Reference ยท Plumbing Commands

git rev-parse command

Reference for git rev-parse: turn branch names and expressions into object IDs, print repository paths with --show-toplevel and --git-dir, and test conditions in scripts.

git rev-parse is the Swiss army knife of scripting with Git. Its most common job is turning a revision expression (HEAD, main~2, v1.0^{tree}) into a full object ID. It also answers questions about the environment: where is the repository root, are we inside a work tree, is this repository shallow, what is the current branch name.

Because its output is plain text with no decoration, it is the standard way to capture a value in a shell variable.

Terminal
git rev-parse HEAD
git rev-parse --short=8 main
git rev-parse --show-toplevel

Try it

Bash Resolve revisions

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Bash Ask about the repository and the current folder

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Syntax

Terminal
git rev-parse [<options>] <args>...
Option Description
<rev> Print the object ID of a revision expression.
--verify Require exactly one valid revision; exit non-zero otherwise. Combine with --quiet to suppress the error message.
--short[=<n>] Abbreviate IDs (default length from core.abbrev, at least 4).
--abbrev-ref[=strict|loose] Print the short ref name (main) instead of an ID.
--symbolic-full-name Print the full ref name (refs/heads/main).
--show-toplevel Absolute path of the working tree root.
--show-prefix, --show-cdup Path from the root to the current folder, and the ../ sequence to go back.
--git-dir, --absolute-git-dir, --git-common-dir Location of the repository (relative or absolute; common dir for linked worktrees).
--is-inside-work-tree, --is-inside-git-dir, --is-bare-repository, --is-shallow-repository Print true or false.
--branches, --tags, --remotes, --all Print the IDs of the refs in that namespace.
--not, ^<rev> Negate the revisions that follow.
--default <rev> Use this revision if none is given.
--parseopt, --sq-quote Helpers for shell scripts that parse options or quote values.

Returns: one line per resolved argument.

Errors: with --verify, exit code 128 and "Needed a single revision" when the name cannot be resolved. Without it, an unknown name is echoed back unchanged and an error is printed, which surprises many scripts.

Usage notes

  • Always use --verify when you need to test whether a revision exists; plain git rev-parse foo prints foo for an unknown name.
  • Peeling: v1^{commit} follows an annotated tag to its commit; ^{tree} goes to the tree. See Peeling syntax.
  • Short IDs are only as unique as the repository requires, so do not store them long-term.
  • Detached HEAD: --abbrev-ref HEAD prints HEAD, not a branch name. Use git symbolic-ref --short -q HEAD to detect that case.
  • Inside .git: --show-toplevel fails there because there is no work tree.
  • Prefer higher-level commands for display (git branch --show-current for the branch name); keep rev-parse for scripts.