How do you get the Git repository's name in some Git repository?
Well, if, for the repository name you mean the Git root directory name (the one that contains the .git directory) you can run this:
basename `git rev-parse --show-toplevel`
The git rev-parse --show-toplevel
part gives you the path to that directory and basename
strips the first part of the path.
In general, you cannot do this. Git does not care how your git repository is named. For example, you can rename directory containing your repository (one with .git
subdirectory), and git will not even notice it - everything will continue to work.
However, if you cloned it, you can use command:
git remote show origin
to display a lot of information about original remote that you cloned your repository from, and it will contain original clone URL.
If, however, you removed link to original remote using git remote rm origin
, or if you created that repository using git init
, such information is simply impossible to obtain - it does not exist anywhere.
There's no need to contact the repository to get the name, and the folder name won't necessarily reflect the remote name.
I've found this to be the most accurate and efficient way to get the current repository name:
basename -s .git `git config --get remote.origin.url`
This should work as of Git 1.8.1.5. Prior to this, the now deprecated git-repo-config
command would have worked (as early as Git 1.7.5).