Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find the path of the local git repository when I am possibly in a subdirectory [duplicate]

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 ~.

like image 482
halloleo Avatar asked Sep 06 '12 06:09

halloleo


People also ask

How do I find my git repository path?

The default location that Git Bash starts in is typically the home directory (~) or /c/users/<Windows-user-account>/ on Windows OS. To determine the current directory, type pwd at the $ prompt. Change directory (cd) into the folder that you created for hosting the repository locally.

How do I navigate to a local git repository?

Step 1: Go to Github repository and in code section copy the URL. Step 2: In the Command prompt, add the URL for your repository where your local repository will be pushed. Step 3: Push the changes in your local repository to GitHub. Here the files have been pushed to the master branch of your repository.


1 Answers

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 
like image 61
VonC Avatar answered Sep 29 '22 13:09

VonC