basin
basin

Reputation: 4190

git: get effective GIT_DIR, if current dir is a working tree subdir and $GIT_DIR is unset

From a script, how to determine the current GIT_DIR? If not overriden, it's just the top level dir or the top level dir of a bare repo.

I want something like:

[/git/repo1/subdir]$ git show-git-dir
/git/repo1/.git

Upvotes: 9

Views: 1237

Answers (2)

PAntoine
PAntoine

Reputation: 689

Here is a pure bash version, and will find the directory when you are in the root. (Ikke's solution is better by the way).

a=$PWD

while [ "$a" != "" ] && [ "$a" != '/' ];
do
    a=`dirname $a`;
    if [ -d $a/.git ];
    then
        echo "$a";
        break;
    fi
done

On windows you will probably have to add another [ "$a" != '\' ] to test for that case, and I dont have access at the moment to check if the bash shell that comes with Msysgit supports "dirname".

But works in Terminator on Ubuntu.

Upvotes: 0

Ikke
Ikke

Reputation: 101241

This can be done with rev-parse:

git rev-parse --git-dir

Upvotes: 14

Related Questions