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

Published: 2026-11-30

Rights: original
Source: https://kairos-2z6s.onrender.com/res/git-rev-parse-full-en
`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.

(Test: 3 tests were run, TE-82-A, TE-82-B and TE-82-C. The expected non-zero exit is TE-82-C (exit status 128). Every row has a recorded log, and the exit statuses and log hashes 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`):

```bash
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:

```bash
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`):

```bash
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:

```bash
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:

```bash
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

- https://git-scm.com/docs/git-rev-parse

## 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.

## Related guides
- [Git Branch Rename: change a branch name](https://kairos-2z6s.onrender.com/res/git-branch-rename-full-en)
- [Git Describe: name a commit from the nearest tag](https://kairos-2z6s.onrender.com/res/git-describe-full-en)
- [Git Commit Fixup: correct an earlier commit](https://kairos-2z6s.onrender.com/res/git-commit-fixup-full-en)
- [git revert: undo a commit by adding a new one](https://kairos-2z6s.onrender.com/res/git-revert-full-en)
- [Git Restore: unstage or discard a file](https://kairos-2z6s.onrender.com/res/git-restore-full-en)
- [git reset: soft, mixed and hard on the same repository](https://kairos-2z6s.onrender.com/res/git-reset-modes-full-en)
