LinuxCommandLibrary

git-rev-parse

Resolve symbolic commit names to object IDs

TLDR

Get the commit hash of a branch

$ git rev-parse [branch_name]
copy

Get the current branch name
$ git rev-parse --abbrev-ref [HEAD]
copy

Get the absolute path to the root directory
$ git rev-parse --show-toplevel
copy

SYNOPSIS

git rev-parse [options] [--] <rev>...

PARAMETERS

--parseopt
    Parse getopt(1)-style options from args

--keep-dashdash
    Keep leading dashes in args

--stop-at-non-option
    Stop at first non-option arg

--sq-quote
    Shell-quote args for eval

--revs-only
    Output only revision args

--no-revs
    Strip revision args

--flags
    Output flag args only

--no-flags
    Strip flag args

--default <name>
    Default rev if none given

--verify
    Verify rev exists (exit 0/1)

--quiet
    Suppress error messages

--short[=<N>]
    Abbreviate to N digits

--not
    Prefix revs with ^ for negation

--all
    All refs (branches/tags/remotes)

--branches[=<pattern>]
    Matching branches

--tags[=<pattern>]
    Matching tags

--remotes[=<pattern>]
    Matching remotes

--glob=<pattern>
    Glob-matching refs

--reflog[=<ref>]
    Reflog entries

--symbols[=<ref>]
    Packsymbolic refs

--stdin
    Read revs from stdin

--since=<commit>
    Commits after date

--after=<commit>
    Commits before date

--symbolic
    Symbolic ref name

--symbolic-full-name
    Full symbolic name

--abbrev-ref[=(strip|no-strip)]
    Abbrev ref name

--disambiguate=<prefix>
    Resolve prefix

--root
    Repository root directory

--show-prefix
    Prefix to top-level

--show-cdup
    Path to cd-up to top

--git-dir
    Git directory path

--is-inside-work-tree
    Boolean: inside worktree

--is-git-dir
    Boolean: current is git dir

--is-bare-repository
    Boolean: repo is bare

--resolve-git-dir <path>
    Canonical git dir

--git-common-dir
    Common git dir path

--show-toplevel
    Absolute top-level dir

--absolute-git-dir
    Absolute git dir

--show-object-format
    Hash algorithm (sha1/sha256)

DESCRIPTION

git rev-parse is a versatile plumbing command in Git that resolves revision identifiers (revs), symbolic references, and extracts repository metadata. It takes revision arguments or options and outputs canonical object names (SHA-1 or SHA-256 hashes), paths, or boolean statuses.

Primarily used in scripts and by other Git porcelain commands, it handles specifiers like HEAD, branch names, tags, HEAD~2, or :/pattern, expanding them to full 40-character object IDs or abbreviated forms. It verifies existence, disambiguates prefixes, and supports filters for refs (branches, tags, remotes).

Repository queries include detecting bare repos, worktree status, Git dir locations, and root paths. Output can be customized for shortness, symbolic names, or quoted for shell use. When reading from stdin or processing multiple args, it processes each independently.

Common in hooks, aliases, and automation for reliable rev resolution without ambiguity. Errors on invalid input, aiding validation. Supports modern features like multi-hash objects.

CAVEATS

Plumbing command; formats may change between Git versions. Use --help for current details. Invalid revs exit with code 1; relies on repository context.

COMMON EXAMPLES

git rev-parse HEAD
Full SHA of current commit.

git rev-parse --abbrev-ref HEAD
Current branch (e.g., main).

git rev-parse --show-toplevel
Absolute repo root.

git rev-parse --verify refs/heads/main^{commit}
Verify annotated commit.

EXIT CODES

0: success.
1: invalid rev or failure.
128: fatal error (e.g., no repo).

HISTORY

Introduced in Git v1.0.0 (April 2005) by Linus Torvalds and early contributors as core plumbing for rev resolution. Evolved with Git's ref model, adding worktree/multi-hash support in v2.5+ (2014) and beyond.

SEE ALSO

git rev-list(1), git show-ref(1), git symbolic-ref(1), git for-each-ref(1), git ls-remote(1)

Copied to clipboard