Harness the Combinatoric Power of Command-Line Tools and Utilities

../Tutorials

Jump to Git Repository Root

Bash git tips

Published May 13, 2024

If you’re working on a project in the CLI and you’ve navigated to a subfolder, you might want a quick way to navigate back to the project root. You can use a combination of pushd and popd to jump around your shell, but there’s a faster way if your project is a Git repository.

The command git rev-parse --show-toplevel will tell you the path to the top-level directory of a repository. You can feed the result of that command to the cd command to jump to that folder:

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

That command is way too long to remember, so create an alias for it.

With the Bash shell, add an alias by adding the following line to ~/.bashrc on Linux or ~/.bash_profile on macOS:

alias cdr='cd $(git rev-parse --show-toplevel)'

If you use zsh, add the alias to ~/.zshrc.

When you open a new terminal window or source your configuration file, you can use the command cdr to jump to the project root.

When working in a large monorepo with subprojects, like sample code for a book or course, this is a huge time saver.