Git - 将代码推送到两个遥控器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14290113/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Git - Pushing code to two remotes
提问by yasith
I have tworemote git repositories. origin
and github
我有两个远程 git 存储库。origin
和github
I push my branch devel
to both repositories.
我将我的分支推devel
送到两个存储库。
git push -u origin devel
git push -u github devel
But then, when I do. git push
It would only get pushed to github
.
但是,当我这样做时。git push
它只会被推到github
.
Is there anyway I can set up my two remotes, so that I can push changes to both repositories with one command ?
无论如何我可以设置我的两个遥控器,以便我可以使用一个命令将更改推送到两个存储库?
回答by jweyrich
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 中,您可以pushurl
为给定的远程添加多个s。使用以下命令将两个pushurl
s添加到您的origin
:
git remote set-url --add --push origin git://original/repo.git
git remote set-url --add --push origin git://another/repo.git
So when you push to origin
, it will push to both repositories.
因此,当您推送到 时origin
,它将推送到两个存储库。
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.
更新 1:Git 1.8.0.1 和 1.8.1(可能还有其他版本)似乎有一个错误,会导致--add
在您第一次使用原始 URL 时替换它,因此您需要使用相同的命令重新添加原始 URL . 这样做git remote -v
应该会显示每个遥控器的当前 URL。
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 overridesthe 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:
更新 2:Git 维护者 Junio C. Hamano 解释了它的设计方式。正在为给定的远程git remote set-url --add --push <remote_name> <url>
添加一个pushurl
,它会覆盖推送的默认 URL。但是,您可以pushurl
为给定的远程添加多个s,然后允许您使用单个git push
. 您可以在下面验证此行为:
$ 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/*
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 Nelsonin 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
(如@Adam Nelson在评论中所建议的那样),或者继续使用origin
,尽管后者名称对此目的的描述性较差. 如果您仍想使用origin
,请跳过以下步骤,并在所有其他步骤中使用origin
代替all
。
So let's add a new remote called all
that we'll reference later when pushing to multiple repositories:
因此,让我们添加一个新的远程调用all
,我们将在稍后推送到多个存储库时引用它:
$ 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
Then let's add a pushurl
to the all
remote, pointing to another repository:
然后让我们pushurl
向all
远程添加一个,指向另一个存储库:
$ 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
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).
这里git remote -v
显示了新pushurl
的推送,所以如果你这样做git push all master
,它只会将master
分支推送到git://another/repo.git
。这显示了如何pushurl
覆盖默认 url (remote.all.url)。
Now let's add another pushurl
pointing to the original repository:
现在让我们添加另一个pushurl
指向原始存储库的内容:
$ 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
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
.
您会看到pushurl
我们添加的两个s 都保留了。现在,单个git push all master
将master
分支推送到两者git://another/repo.git
和git://original/repo.git
。
回答by William Seiti Mizuta
To send to both remote with one command, you can create a alias for it:
要使用一个命令发送到两个遥控器,您可以为其创建一个别名:
git config alias.pushall '!git push origin devel && git push github devel'
With this, when you use the command git pushall
, it will update both repositories.
这样,当您使用命令时git pushall
,它将更新两个存储库。