Git Rev Parse: ask Git for a path or a commit id
git rev-parse --show-toplevel prints the top directory of the working tree. git rev-parse --abbrev-ref HEAD prints the current branch name.
Each command below is the exact command that was run and each output block is copied from the recorded log. Test ids, log hashes and exit statuses are in the evidence table.
TE-82-A: rev-parse prints the top directory and the branch name from a subdirectory
Setup for this run, inside a throwaway directory (mktemp -d):
git init -q .
git symbolic-ref HEAD refs/heads/main
git config user.email test@example.invalid
git config user.name tester
git config commit.gpgsign false
mkdir -p sub; printf 'a\n' > sub/f.txt; git add sub/f.txt; git commit -qm oneCommand:
cd sub; [ "$(git rev-parse --show-toplevel)" = "$(cd .. && pwd -P)" ] && echo top_ok; git rev-parse --abbrev-ref HEADRecorded output:
top_ok
mainExit status: 0. The output matches the expected text written before the run.
TE-82-B: inside a subdirectory, rev-parse reports that it is in a work tree and prints the prefix
Setup for this run, inside a throwaway directory (mktemp -d):
git init -q .
git symbolic-ref HEAD refs/heads/main
git config user.email test@example.invalid
git config user.name tester
git config commit.gpgsign false
mkdir -p sub; printf 'a\n' > sub/f.txt; git add sub/f.txt; git commit -qm oneCommand:
cd sub; git rev-parse --is-inside-work-tree; git rev-parse --show-prefixRecorded output:
true
sub/Exit status: 0. This status and the output match what was written before the run.
TE-82-C: expected non-zero exit: rev-parse --show-toplevel outside any repository stops with a fatal error
No setup needed: the command below creates what it uses in the throwaway folder.
Command:
git rev-parse --show-toplevelRecorded output: none, standard output was empty.
Standard error from the same run:
fatal: not a git repository (or any of the parent directories): .gitExit status: 128. This status and the output match what was written before the run.
TE-82-B: from inside sub, --is-inside-work-tree printed true and --show-prefix printed sub/. TE-82-C: in a folder that is not a repository, git rev-parse --show-toplevel exited with status 128, and standard error reads fatal: not a git repository (or any of the parent directories): .git.
Limits
The run is inside a repository. Outside one, the command fails (TE-82-C). Options other than --show-toplevel, --abbrev-ref, --is-inside-work-tree and --show-prefix were not run. Not tested on this page: the --parseopt and --sq-quote forms of the command. The usage text printed by git rev-parse -h in git 2.34.1 lists them next to the plain form that the runs here used.
Source
Evidence table
| Test | What it checks | Exit | Verdict | Log sha256 (first 12) | |---|---|---|---|---| | TE-82-A | rev-parse prints the top directory and the branch name from a subdirectory | 0 | PASS | 0486dd18468d | | TE-82-B | inside a subdirectory, rev-parse reports that it is in a work tree and prints the prefix | 0 | PASS | 767e304f6a5c | | TE-82-C | expected non-zero exit: rev-parse --show-toplevel outside any repository stops with a fatal error | 128 | PASS | aa591a110e5e |
Recorded on: bash 5.1.16(1)-release; git version 2.34.1; Python 3.10.12. Locale C.UTF-8, TZ=UTC. Every row has a recorded log holding the setup commands, the command, stdout, stderr and exit status. The first 12 characters of the log's sha256 are in the table.
All full guides: All guides