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?
In case anyone needs a POSIX compliant way of doing this, without needing
git
executable:git-root
:To calculate the absolute path of the current git root directory, say for use in a shell script, use this combination of readlink and git rev-parse:
git-rev-parse --show-cdup
gives you the right number of ".."s to get to the root from your cwd, or the empty string if you are at the root. Then prepend "./" to deal with the empty string case and usereadlink -f
to translate to a full path.You could also create a
git-root
command in your PATH as a shell script to apply this technique:(The above can be pasted into a terminal to create git-root and set execute bits; the actual script is in lines 2, 3 and 4.)
And then you'd be able to run
git root
to get the root of your current tree. Note that in the shell script, use "-e" to cause the shell to exit if the rev-parse fails so that you can properly get the exit status and error message if you are not in a git directory.How about "
git rev-parse --git-dir
" ?The
--git-dir
option seems to work.From git rev-parse manual page:
You can see it in action in this
git setup-sh
script.If you are in a submodule folder, with Git 2.20, use:
To amend the "git config" answer just a bit:
and get the path cleaned up. Very nice.
Yes: