问题
When you are working in some Git directory, how can you get the Git repository name in some Git repository? Are there any Git commands?
# I did check out bar repository and working in somewhere
# under bar directory at this moment such as below.
$ git clone git://github.com/foo/bar.git
$ cd bar/baz/qux/quux/corge/grault # and I am working in here!
$ git xxx # <- ???
bar
回答1:
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.
回答2:
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.
回答3:
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).
回答4:
In git v2.7.0+, a subcommand get-url
was introduced to git-remote
command.
POSIX shell:
basename $(git remote get-url origin)
PowerShell:
Split-Path -Leaf (git remote get-url origin)
回答5:
Other answers still won't work when the name of your directory does not correspond to remote repository name (and it could). You can get the real name of the repository with something like this:
git remote show origin -n | grep "Fetch URL:" | sed -E "s#^.*/(.*)$#\1#" | sed "s#.git$##"
Basically, you call git remote show origin
, take the repository URL from "Fetch URL:" field, and regex it to get the portion with name:
https://github.com/dragn/neat-vimrc.git
回答6:
I think this is a better way to unambiguously identify a clone of a repository.
git config --get remote.origin.url
and checking to make sure that the origin matches ssh://your/repo
.
回答7:
A little bit late for this question, but if you:
cat /path/to/repo/.git/config
You will see the url of the repository which will include the reponame:
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
ignorecase = true
precomposeunicode = true
[remote "origin"]
url = https://github.com/your_git_user_name/your_git_repo_name.git
fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
remote = origin
merge = refs/heads/master
回答8:
Repo full name:
git config --get remote.origin.url | grep -Po "(?<=git@github\.com:)(.*?)(?=.git)"
回答9:
If you are trying to get the username or organization name AND the project or repo name on github, I was able to write this command which works for me locally at least.
▶ git config --get remote.origin.url
# => https://github.com/Vydia/gourami.git
▶ git config --get remote.origin.url | sed 's/.*\/\([^ ]*\/[^.]*\).*/\1/' # Capture last 2 path segments before the dot in .git
# => Vydia/gourami
This is the desired result as Vydia
is the organization name and gourami
is the package name. Combined they can help form the complete User/Repo
path
回答10:
Here's a bash function that will print the repository name (if it has been properly set up):
__get_reponame ()
{
local gitdir=$(git rev-parse --git-dir)
if [ $(cat ${gitdir}/description) != "Unnamed repository; edit this file 'description' to name the repository." ]; then
cat ${gitdir}/description
else
echo "Unnamed repository!"
fi
}
Explanation:
local gitdir=$(git rev-parse --git-dir)
This executes git rev-parse --git-dir
, which prints the full path to the .git
directory of the currrent repository. It stores the path in $gitdir
.
if [ $(cat ${gitdir}/description) != "..." ]; then
This executes cat ${gitdir}/description
, which prints the contents of the .git/description
of your current repository. If you've properly named your repository, it will print a name. Otherwise, it will print Unnamed repository; edit this file 'description' to name the repository.
cat ${gitdir}/description
If the repo was properly named, then print the contents.
else
Otherwise...
echo "Unnamed repository!"
Tell the user that the repo was unnamed.
Something similar is implemented in this script.
回答11:
Unfortunately, it seems that Git has no such command built in. But you can easily add it yourself with Git aliases and some shell magic.
As pointed out by this answer, you can use git rev-parse --show-toplevel
to show the root of your current Git folder.
If you want to use this regularly, it's probably more convenient to define it as an alias. For this, used git config alias.root '!echo "$(git rev-parse --show-toplevel)"'
. After this, you can use git root
to see the root folder of the repository you're currently in.
If you want to use another subcommand name than root
, simply replace the second part of alias.root
in the above command with whatever you want.
For details on aliases in Git, see also the git config man page.
回答12:
If you want the whole GitHub repository name ('full name') - user/repository, and you want to do it in with Ruby...
git remote show origin -n | ruby -ne 'puts /^\s*Fetch.*:(.*).git/.match($_)[1] rescue nil'
回答13:
This approach using git-remote
worked well for me for HTTPS remotes:
$ git remote -v | grep "(fetch)" | sed 's/.*\/\([^ ]*\)\/.*/\1/'
| | | |
| | | +---------------+
| | | Extract capture |
| +--------------------+-----+
|Repository name capture|
+-----------------------+
Example
回答14:
Here's mine:
git remote --verbose | grep origin | grep fetch | cut -f2 | cut -d' ' -f1
no better than the others, but I made it a bash function so I can drop in the remote name if it isn't origin.
grurl () {
xx_remote=$1
[ -z "$xx_remote" ] && xx_remote=origin
git remote --verbose | grep "$1" | grep fetch | cut -f2 | cut -d' ' -f1
unset xx_remote
}
回答15:
Also I just find that there is some repo information inside .git
directory. So you can just watch FETCH_HEAD
file in terminal to see repo's name:
Example:
cd your_project_folder/.git
more FETCH_HEAD
Output:
672e38391557a192ab23a632d160ef37449c56ac https://bitbucket.org/fonjeekay/some_repo
And https://bitbucket.org/somebituser/some_repo.git
is the name of your repository
回答16:
You can use: git remote -v
Documentation: https://git-scm.com/docs/git-remote
Manage the set of repositories ("remotes") whose branches you track. -v --verbose Be a little more verbose and show remote url after name. NOTE: This must be placed between remote and subcommand.
来源:https://stackoverflow.com/questions/15715825/how-do-you-get-the-git-repositorys-name-in-some-git-repository