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
| Flag | Meaning |
|---|---|
| --short[=<n>] | abbreviate the hash |
| --abbrev-ref HEAD | print the current branch name |
| --show-toplevel | print the root of the working tree |
| --git-dir | print the path of the .git directory |
| --is-inside-work-tree | true or false, for scripts |
| --verify <rev> | fail loudly if the name does not resolve |
Examples
git rev-parse --short HEADthe short hash a build should stamp itself with
git rev-parse --abbrev-ref HEADthe branch name, for a CI script
git rev-parse --show-toplevelthe 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