问题
I have a git repo that is a fork of another repo. As a rule I will normally add a remote called upstream, which is the original repo I forked from.
$ git remote -v
origin git@github.com:skela/awesomeproject.git (fetch)
origin git@github.com:skela/awesomeproject.git (push)
upstream git://github.com/bob/awesomeproject.git (fetch)
upstream git://github.com/bob/awesomeproject.git (push)
Is there any way to have this additional remote persist across clones? Say I delete my local repository and do a:
git clone git@github.com:skela/awesomeproject.git
And now I recheck my remotes:
$ git remote -v
origin git@github.com:skela/awesomeproject.git (fetch)
origin git@github.com:skela/awesomeproject.git (push)
My upstream remote has vanished!
How to I ensure that my git repo always keeps these 2 remote aliases?
Edit: Just adding the main reason why I want to do this as to shape some of the answers down an acceptable path ;)
Goal is to have a branch in my repo that tracks the upstream's master.
[remote "upstream"]
url = git://github.com/bob/awesomeproject.git
fetch = +refs/heads/*:refs/remotes/upstream/*
[branch "father"]
remote = upstream
merge = refs/heads/master
In other words, the branch "father" which is in my repo tracks the remote called upstream's master branch.
It all works great once I set it up, but as soon as I clone the repo again, the "father" branch points to origin instead of the upstream.
回答1:
That is impossible. Git only clones the repo’s content, never its settings. If your want to hard-wire remotes into your repo (it stands to question whether that is a good idea), create a script repo-setup.sh
in your repo root that does something like this:
git remote rm origin
git remote rm upstream
git remote add origin git@github.com:skela/awesomeproject.git
git remote add upstream git://github.com/bob/awesomeproject.git
Then run this file after you cloned the repository.
回答2:
Create a file .gitremotes that you populate with the content of .git/config related to remotes. Add .gitremotes to the repository. After the clone append .git/config with .gitremotes. Note: might need some hand editing if the remotes that you want to share (in .gitremotes) have a name conflict with the remote that 'git clone' creates automatically ('orgin').
To accomplish this easily you could define a bash function:
function git-clone-r ()
{
src=$1
tgt=$2
git clone $src $tgt
cat ${tgt}/.gitremotes >> ${tgt}/.git/config
}
[The above isn't all that sophisticated; but illustrates the point and works]
回答3:
This is a slightly modified version of GoZoner's solution.
You need to capture the info about all the remotes from your repo's .git/config
into a file that you could store outside your git repository. You also need to take care of updating this file every time you add a new remote. This can in fact be added to your git repo, so that the next clone or pull brings in this file.
Starting with git 1.7.10+
, git supports including external config files.
So you can add the following lines to your repo's .git/config
to include the external config file containing the remote info:
[include]
path = /dir1/dir2/repo-remotes.gitinfo
来源:https://stackoverflow.com/questions/15286307/how-to-make-git-repo-remember-all-remotes