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.
git rev-parse HEAD git rev-parse --short=8 main git rev-parse --show-toplevel
Try it
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.
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
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
--verifywhen you need to test whether a revision exists; plaingit rev-parse fooprintsfoofor 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 HEADprintsHEAD, not a branch name. Usegit symbolic-ref --short -q HEADto detect that case. - Inside
.git:--show-toplevelfails there because there is no work tree. - Prefer higher-level commands for display (
git branch --show-currentfor the branch name); keeprev-parsefor scripts.