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?
Had to solve this myself today. Solved it in C# as I needed it for a program, but I guess it can be esily rewritten. Consider this Public Domain.
As others have noted, the core of the solution is to use
git rev-parse --show-cdup
. However, there are a few of edge cases to address:When the cwd already is the root of the working tree, the command yields an empty string.
Actually it produces an empty line, but command substitution strip off the trailing line break. The final result is an empty string.
Most answers suggest prepending the output with
./
so that an empty output becomes"./"
before it is fed tocd
.When GIT_WORK_TREE is set to a location that is not the parent of the cwd, the output may be an absolute pathname.
Prepending
./
is wrong in this situation. If a./
is prepended to an absolute path, it becomes a relative path (and they only refer to the same location if the cwd is the root directory of the system).The output may contain whitespace.
This really only applies in the second case, but it has an easy fix: use double quotes around the command substitution (and any subsequent uses of the value).
As other answers have noted, we can do
cd "./$(git rev-parse --show-cdup)"
, but this breaks in the second edge case (and the third edge case if we leave off the double quotes).Many shells treat
cd ""
as a no-op, so for those shells we could docd "$(git rev-parse --show-cdup)"
(the double quotes protect the empty string as an argument in the first edge case, and preserve whitespace in the third edge case). POSIX says the result ofcd ""
is unspecified, so it may be best to avoid making this assumption.A solution that works in all of the above cases requires a test of some sort. Done explicitly, it might look like this:
No
cd
is done for the first edge case.If it is acceptable to run
cd .
for the first edge case, then the conditional can be done in the expansion of the parameter:I wanted to expand upon Daniel Brockman's excellent comment.
Defining
git config --global alias.exec '!exec '
allows you to do things likegit exec make
because, asman git-config
states:It's also handy to know that
$GIT_PREFIX
will be the path to the current directory relative to the top-level directory of a repository. But, knowing it is only half the battle™. Shell variable expansion makes it rather hard to use. So I suggest usingbash -c
like so:other commands include:
The
man
page forgit-config
(under Alias) says:So, on UNIX you can do:
If you're already in the top-level or not in a git repository
cd $(git rev-parse --show-cdup)
will take you home (just cd).cd ./$(git rev-parse --show-cdup)
is one way of fixing that.If you're looking for a good alias to do this plus not blow up
cd
if you aren't in a git dir: