How to make git repo remember all remotes?

非 Y 不嫁゛ 提交于 2019-11-30 07:02:59

That is impossible. Git only clones the repo’s content, never it’s settings. If your want to hard-wire remotes into your repo (it stands to question whether this 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

The run this file after you cloned the repository.

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]

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!