Git - 将代码推送到两个遥控器 [英] Git - Pushing code to two remotes
问题描述
我有两个远程 git 存储库.origin
和 github
I have two remote git repositories. origin
and github
我将我的分支 devel
推送到两个存储库.
I push my branch devel
to both repositories.
git push -u origin devel
git push -u github devel
但是,当我这样做时.git push
它只会被推送到 github
.
But then, when I do. git push
It would only get pushed to github
.
无论如何我可以设置我的两个遥控器,以便我可以使用一个命令将更改推送到两个存储库吗?
Is there anyway I can set up my two remotes, so that I can push changes to both repositories with one command ?
推荐答案
在最近的 Git 版本中,您可以为给定的远程添加多个 pushurl
.使用以下内容将两个 pushurl
添加到您的 origin
:
In recent versions of Git you can add multiple pushurl
s for a given remote. Use the following to add two pushurl
s to your origin
:
git remote set-url --add --push origin git://original/repo.git
git remote set-url --add --push origin git://another/repo.git
因此,当您推送到 origin
时,它会推送到两个存储库.
So when you push to origin
, it will push to both repositories.
UPDATE 1:Git 1.8.0.1 和 1.8.1(可能还有其他版本)似乎有一个错误,导致 --add
替换第一次使用原始 URL,因此需要使用相同的命令重新添加原始 URL.执行 git remote -v
应该会显示每个遥控器的当前 URL.
UPDATE 1: Git 1.8.0.1 and 1.8.1 (and possibly other versions) seem to have a bug that causes --add
to replace the original URL the first time you use it, so you need to re-add the original URL using the same command. Doing git remote -v
should reveal the current URLs for each remote.
更新 2: Git 维护者 Junio C. Hamano 解释了它的设计方式.执行 git remote set-url --add --push
为给定的远程添加一个 pushurl
,它覆盖推送的默认 URL.但是,您可以为给定的远程添加多个 pushurl
,然后允许您使用单个 git push
推送到多个远程.您可以在下面验证此行为:
UPDATE 2: Junio C. Hamano, the Git maintainer, explained it's how it was designed. Doing git remote set-url --add --push <remote_name> <url>
adds a pushurl
for a given remote, which overrides the default URL for pushes. However, you may add multiple pushurl
s for a given remote, which then allows you to push to multiple remotes using a single git push
. You can verify this behavior below:
$ git clone git://original/repo.git
$ git remote -v
origin git://original/repo.git (fetch)
origin git://original/repo.git (push)
$ git config -l | grep '^remote.'
remote.origin.url=git://original/repo.git
remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*
现在,如果您想使用单个命令推送到两个或多个存储库,您可以创建一个名为 all
的新远程(如 @Adam Nelson 在评论中),或继续使用 origin
,尽管后者的名称对此目的的描述性较差.如果您仍想使用origin
,请跳过以下步骤,并在所有其他步骤中使用origin
而不是all
.
Now, if you want to push to two or more repositories using a single command, you may create a new remote named all
(as suggested by @Adam Nelson in comments), or keep using the origin
, though the latter name is less descriptive for this purpose. If you still want to use origin
, skip the following step, and use origin
instead of all
in all other steps.
所以让我们添加一个名为 all
的新远程,我们将在稍后推送到多个存储库时引用它:
So let's add a new remote called all
that we'll reference later when pushing to multiple repositories:
$ git remote add all git://original/repo.git
$ git remote -v
all git://original/repo.git (fetch) <-- ADDED
all git://original/repo.git (push) <-- ADDED
origin git://original/repo.git (fetch)
origin git://original/repo.git (push)
$ git config -l | grep '^remote.all'
remote.all.url=git://original/repo.git <-- ADDED
remote.all.fetch=+refs/heads/*:refs/remotes/all/* <-- ADDED
然后让我们添加一个 pushurl
到 all
远程,指向另一个仓库:
Then let's add a pushurl
to the all
remote, pointing to another repository:
$ git remote set-url --add --push all git://another/repo.git
$ git remote -v
all git://original/repo.git (fetch)
all git://another/repo.git (push) <-- CHANGED
origin git://original/repo.git (fetch)
origin git://original/repo.git (push)
$ git config -l | grep '^remote.all'
remote.all.url=git://original/repo.git
remote.all.fetch=+refs/heads/*:refs/remotes/all/*
remote.all.pushurl=git://another/repo.git <-- ADDED
这里git remote -v
显示了新的pushurl
用于推送,所以如果你做git push all master
,它会推送master
仅分支到 git://another/repo.git
.这显示了 pushurl
如何覆盖默认 url (remote.all.url).
Here git remote -v
shows the new pushurl
for push, so if you do git push all master
, it will push the master
branch to git://another/repo.git
only. This shows how pushurl
overrides the default url (remote.all.url).
现在让我们添加另一个指向原始存储库的pushurl
:
Now let's add another pushurl
pointing to the original repository:
$ git remote set-url --add --push all git://original/repo.git
$ git remote -v
all git://original/repo.git (fetch)
all git://another/repo.git (push)
all git://original/repo.git (push) <-- ADDED
origin git://original/repo.git (fetch)
origin git://original/repo.git (push)
$ git config -l | grep '^remote.all'
remote.all.url=git://original/repo.git
remote.all.fetch=+refs/heads/*:refs/remotes/all/*
remote.all.pushurl=git://another/repo.git
remote.all.pushurl=git://original/repo.git <-- ADDED
你看到我们添加的两个 pushurl
都被保留了.现在一个 git push all master
会将 master
分支推送到 git://another/repo.git
和 git://original/repo.git
.
You see both pushurl
s we added are kept. Now a single git push all master
will push the master
branch to both git://another/repo.git
and git://original/repo.git
.
重要说明:如果您的遥控器有不同的规则(挂钩)来接受/拒绝推送,一个遥控器可能会接受它,而另一个则不会.因此,如果您希望它们具有完全相同的历史记录,则需要在本地修复您的提交以使它们被两个遥控器接受并再次推送,否则您最终可能会遇到只能通过重写历史记录来修复它的情况(使用 push -f
),这可能会给已经从 repo 中提取您之前更改的人带来问题.
IMPORTANT NOTE: If your remotes have distinct rules (hooks) to accept/reject a push, one remote may accept it while the other doesn't. Therefore, if you want them to have the exact same history, you'll need to fix your commits locally to make them acceptable by both remotes and push again, or you might end up in a situation where you can only fix it by rewriting history (using push -f
), and that could cause problems for people that have already pulled your previous changes from the repo.
这篇关于Git - 将代码推送到两个遥控器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!