问题
When you do your first clone using the syntax
git clone username@server:gitRepo.git
Is it possible using your local repository to find the name of that initial clone?
(So in the above example, find gitRepo.git
.)
回答1:
In the repository root, the .git/config
file holds all information about remote repositories and branches. In your example, you should look for something like:
[remote "origin"]
fetch = +refs/heads/*:refs/remotes/origin/*
url = server:gitRepo.git
Also, the Git command git remote -v
shows the remote repository name and URL. The "origin" remote repository usually corresponds to the original repository, from which the local copy was cloned.
回答2:
git config --get remote.origin.url
回答3:
This is quick Bash command, that you're probably searching for, will print only a basename of the remote repository:
Where you fetch from:
basename $(git remote show -n origin | grep Fetch | cut -d: -f2-)
Alternatively where you push to:
basename $(git remote show -n origin | grep Push | cut -d: -f2-)
Especially the -n
option makes the command much quicker.
回答4:
I use this:
basename $(git remote get-url origin) .git
Which returns something like gitRepo
. (Remove the .git
at the end of the command to return something like gitRepo.git
.)
(Note: It requires Git version 2.7.0 or later)
回答5:
git remote show origin -n | ruby -ne 'puts /^\s*Fetch.*(:|\/){1}([^\/]+\/[^\/]+).git/.match($_)[2] rescue nil'
It was tested with three different URL styles:
echo "Fetch URL: http://user@pass:gitservice.org:20080/owner/repo.git" | ruby -ne 'puts /^\s*Fetch.*(:|\/){1}([^\/]+\/[^\/]+).git/.match($_)[2] rescue nil'
echo "Fetch URL: Fetch URL: git@github.com:home1-oss/oss-build.git" | ruby -ne 'puts /^\s*Fetch.*(:|\/){1}([^\/]+\/[^\/]+).git/.match($_)[2] rescue nil'
echo "Fetch URL: https://github.com/owner/repo.git" | ruby -ne 'puts /^\s*Fetch.*(:|\/){1}([^\/]+\/[^\/]+).git/.match($_)[2] rescue nil'
回答6:
I stumbled on this question trying to get the organization/repo
string from a git host like github or gitlab.
This is working for me:
git config --get remote.origin.url | sed -e 's/^git@.*:\([[:graph:]]*\).get/\1/'
It uses sed
to replace the output of the git config
command with just the organization and repo name.
Something like github/scientist
would be matched by the character class [[:graph:]]
in the regular expression.
The \1
tells sed to replace everything with just the matched characters.
来源:https://stackoverflow.com/questions/4076239/finding-out-the-name-of-the-original-repository-you-cloned-from-in-git