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] <arg>...

PARAMETERS

--quiet, -q
    Only output SHA-1 on stdout; suppress all error messages.

--verify
    Ensure that the arguments are valid Git object names. If not, exit with a non-zero status.

--short[=length]
    Shortens the object names to a unique prefix. If length is specified, it tries to shorten to that length.

--symbolic-full-name
    If the argument is a symbolic ref (e.g., `HEAD`), output its full name (e.g., `refs/heads/master`) instead of the SHA-1.

--abbrev-ref[=length]
    Abbreviates references. For example, `refs/heads/master` becomes `master`.

--is-bare-repository
    Checks if the current repository is a bare repository (no working tree). Exits with 0 if true, 1 otherwise.

--is-inside-git-dir
    Checks if the current working directory is inside the `.git` directory. Exits with 0 if true, 1 otherwise.

--is-inside-work-tree
    Checks if the current working directory is inside the Git work tree. Exits with 0 if true, 1 otherwise.

--show-cdup
    Shows the path from the current directory to the top-level directory of the work tree.

--show-prefix
    Shows the path from the top-level directory of the work tree to the current directory.

--show-toplevel
    Shows the absolute path to the top-level directory of the work tree.

--glob=glob-pattern
    Expands glob patterns into a list of matching refs. Useful for finding all branches or tags that match a pattern.

--sq-quote
    Quote all arguments for use in a shell. Useful for passing complex arguments safely.

--default arg
    If no arguments are given or they do not specify a revision, output arg.

DESCRIPTION

The git rev-parse command is a low-level "plumbing" command in Git, primarily used for converting various human-friendly revision expressions into their corresponding Git object SHA-1 hashes. It is a powerful tool for scripting and internal Git operations, enabling programs to reliably determine the Git object ID for a given reference (like a branch name, tag, or HEAD).

Beyond resolving object names, it can also parse command-line options, resolve paths relative to the Git repository, and provide information about the repository's state (e.g., whether it's bare, or if the current directory is within the `.git` directory). Its output is typically machine-readable, making it invaluable for building robust Git-aware scripts that need to interact directly with Git's object database.

CAVEATS

git rev-parse is a low-level command; its output is primarily intended for machine consumption (e.g., scripts). Its behavior can sometimes be subtle, especially when dealing with ambiguous references or paths. Relying on its output directly in user-facing contexts without proper validation is generally not recommended. Internal Git behavior that `rev-parse` reflects may change in future versions.

SPECIFYING REVISIONS

git rev-parse supports a rich set of revision notations, including:

  • SHA-1 object names (full or abbreviated).
  • Branch names (e.g., `master`, `main`).
  • Tag names (e.g., `v1.0`).
  • `HEAD` (the current commit of the active branch).
  • Relative revisions (e.g., `HEAD~3` for the 3rd ancestor, `HEAD^` for the first parent).
  • References to remote branches (e.g., `origin/master`).
  • @{date} or @{n} for looking up refs by time or position in reflog.
  • @{u} or @{upstream} for the upstream branch.
  • Ranges like `A..B` for commits reachable from B but not A.
  • Paths combined with revisions (e.g., `HEAD:path/to/file` to get the blob ID).

This flexibility is central to its utility, allowing precise targeting of Git objects.

OUTPUT FORMATS

The command's output varies significantly depending on the options used:

  • By default, it outputs the full 40-character SHA-1 hash for the resolved object.
  • With `--short`, it outputs an abbreviated SHA-1.
  • With `--symbolic-full-name` or `--abbrev-ref`, it outputs the symbolic name of the reference if applicable.
  • Options like `--show-cdup`, `--show-prefix`, `--show-toplevel` output paths relative to the current directory or the work tree root.
  • Boolean options like `--is-bare-repository` simply set the exit status (0 for true, 1 for false) and typically produce no standard output, or optionally a `true`/`false` line if combined with other options.
  • `--glob` expands patterns into a list of matching full ref names, one per line.

Understanding the expected output for specific options is crucial for effective scripting.

HISTORY

git rev-parse has been a fundamental "plumbing" command since the early days of Git, forming a core part of its internal machinery for resolving and understanding revisions. Its robust ability to translate various human-readable inputs into unambiguous object IDs has made it indispensable for Git's own commands and for external scripting. It has evolved with Git, incorporating new ways to specify revisions and providing more utilities for path resolution and repository state queries, solidifying its role as a bedrock utility in the Git ecosystem.

SEE ALSO

Copied to clipboard