Monday, May 25, 2015

git: handy alias to find the repository root

To quickly move to the root of the current git repository, I set up this alias:

git config --global alias.home 'rev-parse --show-toplevel'

Now,  git home prints the full path to the root directory of the current project.
To go there, type (Mac/Linux only)

cd `git home`

Notice the backticks. They're not single quotes. This executes the command and then uses its output as the argument to cd.

This trick is particularly useful in Scala, where I have to get to the project root to run sbt compile. (Things that make me miss Clojure!)

BONUS: handy alias to find the current branch

git config --global alias.whereami "rev-parse --abbrev-ref HEAD"

As in,

git push -u origin `git whereami`


  1. I always used `git rev-parse --show-toplevel`

  2. I'd create a git-home script so that the "git home" command brings me to the top-level directory of the repo.