Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 452
wojo Avatar asked Jun 05 '09 20:06

wojo


People also ask

How do I find my git root directory?

From git rev-parse manual page: --git-dir Show $GIT_DIR if defined else show the path to the . git directory. You can see it in action in this git setup-sh script.

How do I find my git repository in CMD?

You can view that origin with the command git remote -v, which will list the URL of the remote repo.

What is the root directory of the repository?

The repository root directory is the parent directory of the . hg directory. Mercurial stores its internal data structures – the metadata – inside that . hg directory.


Video Answer


1 Answers

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.

like image 179
baudtack Avatar answered Oct 03 '22 05:10

baudtack