KKAIROS
Practical guideIllustrative3 min read

Git Rev Parse: ask Git for a path or a commit id

Ask Git where you are instead of guessing: git rev-parse --show-toplevel prints the repository root and --abbrev-ref HEAD prints the branch name.

Published:

Updated:

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.

How do I get the top directory of the repository?

git rev-parse --show-toplevel prints it. From a subdirectory, plain rev-parse prints the top directory and the branch name.

How do I know whether I am in a work tree?

Inside a subdirectory, rev-parse reports that it is in a work tree and prints the prefix of the current directory inside it.

Why does rev-parse --show-toplevel fail outside a repository?

There is no work tree to report. Outside any repository it stops with a fatal error.

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 one

Command:

cd sub; [ "$(git rev-parse --show-toplevel)" = "$(cd .. && pwd -P)" ] && echo top_ok; git rev-parse --abbrev-ref HEAD

Recorded output:

top_ok
main

Exit 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 one

Command:

cd sub; git rev-parse --is-inside-work-tree; git rev-parse --show-prefix

Recorded 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-toplevel

Recorded output: none, standard output was empty.

Standard error from the same run:

fatal: not a git repository (or any of the parent directories): .git

Exit 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

TestWhat it checksExitVerdictLog sha256 (first 12)
TE-82-Arev-parse prints the top directory and the branch name from a subdirectory0PASS0486dd18468d
TE-82-Binside a subdirectory, rev-parse reports that it is in a work tree and prints the prefix0PASS767e304f6a5c
TE-82-Cexpected non-zero exit: rev-parse --show-toplevel outside any repository stops with a fatal error128PASSaa591a110e5e

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