Is there a way to get the git root directory in one command?

Yes:

git rev-parse --show-toplevel

If you want to replicate the Mercurial command more directly, you can create an alias:

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

and now git root will function just as hg root.


Note: In a submodule this will display the root directory of the submodule and not the parent repository. If you are using Git >=2.13 or above, there is a way that submodules can show the superproject’s root directory. If your git is older than that, see this other answer.

Leave a Comment