Mercurial has a way of printing the root directory (that contains .hg) via
hg root
Is there something equivalent in git to get the director
$ git config alias.root '!pwd'
# then you have:
$ git root
Here is a script that I've written that handles both cases: 1) repository with a workspace, 2) bare repository.
https://gist.github.com/jdsumsion/6282953
git-root
(executable file in your path):
#!/bin/bash
GIT_DIR=`git rev-parse --git-dir` &&
(
if [ `basename $GIT_DIR` = ".git" ]; then
# handle normal git repos (with a .git dir)
cd $GIT_DIR/..
else
# handle bare git repos (the repo IS a xxx.git dir)
cd $GIT_DIR
fi
pwd
)
Hopefully this is helpful.
Since Git 2.13.0, it supports a new option to show the path of the root project, which works even when being used from inside a submodule:
git rev-parse --show-superproject-working-tree
If you use a shell framework, there might already be a shell alias available:
$ grt
in oh-my-zsh (68k) (cd $(git rev-parse --show-toplevel || echo ".")
)$ git-root
in prezto (8.8k) (displays the path to the working tree root)$ g..
zimfw (1k) (changes the current directory to the top level of the working tree.)