git a quick command to go to root of the working tree
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
When you are deep inside a repository, it is often useful to jump straight back to the top-level directory before running project-wide commands. Git already knows where the working tree root is, so the shortest reliable solution is to ask Git for that path and feed it to cd.
The Core Command
The usual one-liner is:
git rev-parse --show-toplevel prints the absolute path to the root of the current working tree. Wrapping it in command substitution lets the shell change into that directory.
This works from almost anywhere inside the repository, including nested subdirectories.
For example, if you are here:
and the repository root is:
then the command changes your shell directly to /Users/me/project.
Why git rev-parse Is Better Than Shell Guessing
You could try to walk upward until you find .git, but repositories are not always simple directory trees. Worktrees, submodules, and some Git layouts make manual guessing less reliable than asking Git directly.
This command is correct because Git itself resolves the repository structure:
That makes it safer than hardcoded relative paths such as cd ../../.., which break as soon as you are in a different depth.
Useful Variations
Sometimes you do not want to change directory immediately. You may just want to capture the path for a script:
That is useful when a script needs to reference files relative to the repository root.
Another related option is --show-cdup, which prints how many .. segments you would need to move back to the top-level directory:
If you are in src/api, the output might look like this:
That can be handy in scripts, but --show-toplevel is usually more convenient for interactive shell use because it gives the full absolute path.
Turn It Into a Shell Function or Alias
Typing the full command every time gets old. A shell function is usually the cleanest improvement:
Add that to your shell startup file and reload it. Then, from anywhere inside a repository, run:
If you prefer a Git alias, you can create one that prints the path:
Then use it like this:
A shell function is still better if your real goal is changing directory, because Git aliases cannot directly change the parent shell's working directory.
Handle the Non-Repository Case Cleanly
The one-liner assumes you are inside a Git working tree. Outside a repository, Git returns an error and cd receives an empty or invalid value.
A safer shell function checks for that:
That version fails clearly instead of producing a confusing shell error.
Submodules and Worktrees
The same command works inside submodules, but it returns the top level of the submodule repository, not the superproject. That is usually the right behavior, because a submodule is its own Git repository.
Similarly, in a linked worktree, git rev-parse --show-toplevel resolves the root of the current worktree, which is what you usually want for local commands.
That is another reason to prefer Git's own repository metadata instead of trying to infer the root manually.
Common Pitfalls
The most common mistake is using a Git alias and expecting it to change the current shell directory directly, which it cannot do by itself. Another is running cd "$(git rev-parse --show-toplevel)" outside a repository and then being surprised by the failure. Developers also sometimes use relative guesses such as cd ../../.., which become brittle as soon as the directory depth changes. A final issue is forgetting that inside a submodule the reported top level belongs to the submodule, not to the parent repository.
Summary
- '
cd "$(git rev-parse --show-toplevel)"is the standard quick jump to the repository root.' - '
git rev-parseis more reliable than manually walking up the directory tree.' - A shell function is the best way to make the command convenient for daily use.
- Add basic error handling if you want it to fail cleanly outside a repository.
- In submodules and worktrees, Git returns the top level of the current repository context.
Related reading
- git add adding ignored files
- Git add and commit in one command
- git add, commit and push commands in one?
- git add only modified changes and ignore untracked files
- git add only modified changes and ignore untracked files
- Git adding files to repo
- git ahead/behind info between master and branch?
- Git alias with positional parameters
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.