89

I'm looking for something like git list-path printing the path of the associated repository (the .git directory).

A bit of background: I have set up git version control on quite a few of my projects/folders. Some of them are nested, so one directory with echo own repository is a subfolder to another directory tracked with another repository.

E.g. in my home directory (~) I use git to track my dot files, but in folder ~/photo/meta-mix/ I have another project I track with a separate repository. Now, say,I have set up another project in directory ~/photo/meta-match/, but I don't know anymore whether it has its own repository. So I want to find out whether this directory is version controlled and where its repository is.

How can I do this with a git command? git status can give me

nothing to commit

in both cases, when ~/photo/meta-match/ has its own repository or when it just refers to the repository of ~.

halloleo
  • 9,216
  • 13
  • 64
  • 122

1 Answers1

126
git rev-parse --show-toplevel

could be enough if executed within a git repo.
From git rev-parse man page:

--show-toplevel

Show the absolute path of the top-level directory.

For older versions (before 1.7.x), the other options are listed in "Is there a way to get the git root directory in one command?":

git rev-parse --git-dir

That would give the path of the .git directory.


The OP mentions:

git rev-parse --show-prefix

which returns the local path under the git repo root. (empty if you are at the git repo root)


Note: for simply checking if one is in a git repo, I find the following command quite expressive:

git rev-parse --is-inside-work-tree

And yes, if you need to check if you are in a .git git-dir folder:

git rev-parse --is-inside-git-dir
Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 1
    Unfortunately on older versions of `git` (before v1.7.0) that just prints `--show-toplevel` -- it's not even an error. – Ben Jackson Sep 06 '12 at 06:21
  • @BenJackson any chance to upgrade? If not, I have edited to add another command – VonC Sep 06 '12 at 06:28
  • Not my question: I'm just commenting on the answer. Given the old behavior I can see real confusion if someone released a script relying on that method. – Ben Jackson Sep 06 '12 at 06:29
  • @BenJackson Ok, I have added another option. – VonC Sep 06 '12 at 06:31
  • Reading up on `rev-parse` I found another good one in this regards: `git rev-parse --show-prefix`: it is empty when I here have a git repository. – halloleo Sep 06 '12 at 07:21
  • @halloleo: Interesting. I have added it to the answer for more visibility. I have also added more commands about simply checking if one is in a git repo. – VonC Sep 06 '12 at 07:30
  • git rev-parse --show-toplevel is empty if I run it from bare repository. It gives me absolute path perfectly fine from a non-bare repo. Any reasons as to why it might be happening? @VonC – Mudassir Razvi Dec 22 '15 at 12:37
  • 2
    @MudassirRazvi because it expects a working tree, and bare repos have none. For bare repo top folder, see http://stackoverflow.com/a/5584084/6309. – VonC Dec 22 '15 at 12:43
  • Perfect! This is exactly what I wanted! I am also writing a post-receive hook! You Save lives @VonC as always! :) – Mudassir Razvi Dec 22 '15 at 12:45
  • @MudassirRazvi Well..., in this instance, Mark Longair did save lives (http://stackoverflow.com/users/223092/mark-longair) ;) – VonC Dec 22 '15 at 13:23
  • Even on newer versions of git (I have 2.30.1), typing something invalid here doesn't give you an error. I mistakenly entered `--show-top-level` instead of `--show-toplevel` and just got "--show-top-level" as output, which was confusing. Type your commands correctly! – Michael Mar 04 '21 at 22:53