This command resolves branch names, tags, revisions, or path expressions into their corresponding SHA-1 hashes or directory paths, making it invaluable for scripting and plumbing tasks in Git.
By default it takes a revision or variable (e.g. HEAD
, master
, refs/tags/v1.0
) and prints the full object name. With flags like --short
it abbreviates the hash; with --abbrev-ref
it shows the branch name instead of a SHA; --verify
ensures the ref exists; --show-toplevel
prints the top-level project directory; and --git-dir
or --work-tree
reveals repository layout paths.
Common variations include git rev-parse --short HEAD
for a shortened commit ID, git rev-parse --abbrev-ref HEAD
to get the current branch, git rev-parse --show-toplevel
to find the project root, and git rev-parse --verify <ref>
to validate a reference.
Similar plumbing commands are git show-ref
for listing refs, git symbolic-ref
for resolving symbolic refs, and git describe
for human-friendly names.