DEV Community

Brian P. Hogan
Brian P. Hogan

Posted on • Originally published at smallsharpsoftwaretools.com

Jump to Git Repository Root

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.

What You Need

  • Git installed on your machine.

Creating a Custom Command

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) 
Enter fullscreen mode Exit fullscreen mode

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)' 
Enter fullscreen mode Exit fullscreen mode

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.

Conclusion

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

Top comments (0)