Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git command to see where is the active GIT_DIR

Tags:

git

Since the git command can be run from any subdirectory of the repo, or even from outside the directory hierarchy of the repo, I'm looking for a git command that'll show me the location of the repo. Essentially, parent directory of the repo's .git/ directory.

For example, if I have a directory structure as follows, I want the output of the command to be /Users/gurjeet/dev/project_repo/, for all the commands shown after the sample directory tree.

/Users/gurjeet/
├── some_directory/
└── dev/
    └── project_repo/
        ├── .git/
        └── src/
$ cd /Users/gurjeet/dev/project_repo
$ git --where-is-.git

$ cd /Users/gurjeet/dev/project_repo/src
$ git --where-is-.git

$ cd /Users/gurjeet/dev/
$ export GIT_DIR='/Users/gurjeet/dev/project_repo/src'
$ git --where-is-.git

$ cd /Users/gurjeet/dev/
$ export GIT_DIR='/Users/gurjeet/dev/project_repo/src'
$ git --where-is-.git

$ cd /tmp
$ export GIT_DIR='/Users/gurjeet/dev/project_repo/src'
$ git --where-is-.git
like image 483
Gurjeet Singh Avatar asked May 08 '26 00:05

Gurjeet Singh


1 Answers

I believe what I was looking for is provided by git rev-parse --absolute-git-dir, or more accurately by the following command:

$ dirname $(GIT_DIR=~/dev/POSTGRES/.git git rev-parse --absolute-git-dir)

A few sample runs:

$ cd ~/dev/Q.HT/public/quote_of_the_day
$ dirname $(GIT_DIR=~/dev/POSTGRES/.git git rev-parse --absolute-git-dir)

/Users/gurjeet/dev/POSTGRES

$ cd ../../../POSTGRES/
$ dirname  $(GIT_DIR=~/dev/POSTGRES/.git git rev-parse --absolute-git-dir)

/Users/gurjeet/dev/POSTGRES

$ cd src
$ dirname  $(GIT_DIR=~/dev/POSTGRES/.git git rev-parse --absolute-git-dir)

/Users/gurjeet/dev/POSTGRES

Some Google foo lead me to Get the path where git alias was run from, which lead me to read git rev-parse --help, which had all the info I needed.

like image 78
Gurjeet Singh Avatar answered May 09 '26 18:05

Gurjeet Singh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!