Home·Terminal commands

git rev-parse

git

Usage

git rev-parse [--short] <rev>

Turns a name like HEAD or v1.2.0^ into a real hash and answers where the repository is; scripts get the branch name with --abbrev-ref HEAD, and outside a repository it exits with an error.

Common flags

FlagMeaning
--short[=<n>]abbreviate the hash
--abbrev-ref HEADprint the current branch name
--show-toplevelprint the root of the working tree
--git-dirprint the path of the .git directory
--is-inside-work-treetrue or false, for scripts
--verify <rev>fail loudly if the name does not resolve

Examples

git rev-parse --short HEAD

the short hash a build should stamp itself with

git rev-parse --abbrev-ref HEAD

the branch name, for a CI script

git rev-parse --show-toplevel

the repository root, wherever you are inside it

The confusion is that undo has several shapes — moving where a branch points is not the same as adding a commit that reverses it.

How to read this

  • Square brackets [ ] mark a part you may leave out.
  • An ellipsis … means you can list more than one.
  • Flags are case-sensitive — in some commands -r and -R do different things.

Questions

Q. What does git rev-parse do?

Turns a name like HEAD or v1.2.0^ into a real hash and answers where the repository is; scripts get the branch name with --abbrev-ref HEAD, and outside a repository it exits with an error.

Q. How do I type it?

git rev-parse [--short] <rev> — square brackets mark the parts you can leave out.

Q. How many flags are worth knowing?

6 are listed here; the full set is in man git. This command sits under git.

Related commands

man page: man git-rev-parse