Reputation: 2817
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
Upvotes: 3
Views: 405
Reputation: 2817
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.
Upvotes: 4