Mercurial has a way of printing the root directory (that contains .hg) via
hg root
Is there something equivalent in git to get the directory that contains the .git directory?
Mercurial has a way of printing the root directory (that contains .hg) via
hg root
Is there something equivalent in git to get the directory that contains the .git directory?
git-extras
adds
$ git root
see https://github.com/tj/git-extras/blob/master/Commands.md#git-root
Availability of git-extras
$ brew install git-extras
$ apt-get install git-extras
To write a simple answer here, so that we can use
to do the job, simply configure your git by using
and then you might want to add the following to your
~/.bashrc
:so that you can just use
cdroot
to go to the top of your repo.Everything else fails at some point either going to the home directory or just miserably failing. This is the quickest and shortest way to get back to the GIT_DIR.
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):Hopefully this is helpful.
Pre-Configured Shell Aliases in Shell Frameworks
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.)Just in case if you're feeding this path to the Git itself, use
:/