git
command line
git tips
version control
development

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.

Browse interview questions

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:

bash
cd "$(git rev-parse --show-toplevel)"

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:

text
/Users/me/project/src/api/controllers

and the repository root is:

text
/Users/me/project

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:

bash
git rev-parse --show-toplevel

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:

bash
repo_root="$(git rev-parse --show-toplevel)"
echo "$repo_root"

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:

bash
git rev-parse --show-cdup

If you are in src/api, the output might look like this:

text
../../

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:

bash
croot() {
  cd "$(git rev-parse --show-toplevel)"
}

Add that to your shell startup file and reload it. Then, from anywhere inside a repository, run:

bash
croot

If you prefer a Git alias, you can create one that prints the path:

bash
git config --global alias.root 'rev-parse --show-toplevel'

Then use it like this:

bash
cd "$(git root)"

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:

bash
1croot() {
2  local root
3  root="$(git rev-parse --show-toplevel 2>/dev/null)" || {
4    echo "Not inside a Git repository" >&2
5    return 1
6  }
7  cd "$root"
8}

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-parse is 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
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Browse interview questions