Finding out the name of the original repository yo

2019-03-07 23:22发布

问题:

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 repository root, .git/config file holds all information about remotes 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, git command: git remote -v shows remote repository name and url. "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 search 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 -n option make 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 retun something like gitRepo.git.)

(Note: Requires git >= 2.7.0)



回答5:

git remote show origin -n | ruby -ne 'puts /^\s*Fetch.*(:|\/){1}([^\/]+\/[^\/]+).git/.match($_)[2] rescue nil'

tested with 3 url style:

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'