Cd to Git root directory

Sometimes you are very deep in your project structure (e.g. ~/code/private/some_app/spec/lib/some_module/come_class_spec.rb) and need to jump to the project root. There is no easy way to do that out of the box. Either you change to a relative path like cd ~/code/private/some_app or you need to move up the hierarchy with cd ../../../. But there is a neat trick, so that you can just type cdr (cd to root directory) and you are in the project root directory. The only requirement is that you are working with a git repository.

Just put this line of code in your ~/.bashrc or aliases script:

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

Then you can always change to the root directory of your git project. If the current directory is not part of a git repository you will get an error message fatal: Not a git repository (or any of the parent directories): .git.

Links & Sources

About Me

Martin Stoev

I work as a Team Lead Engineering at XING and contributed as an Engineer to the Profile, Groups and Startpage.

I am passionate about the projects I work on and very competitative when it comes to sports or games.

Recent Posts

Featured Posts