Git - 将代码推送到两个遥控器 [英] Git - Pushing code to two remotes
问题描述
我有两个远程git存储库。 origin
和 github
我推送我的分支 devel
到这两个存储库。
git push -u origin devel
git push -u github devel
但是,当我这样做时。 git push
它只会被推送到 github
。
有无论如何我可以设置我的两个遥控器,以便我可以用一个命令将更改推送到两个存储库? 解决方案
在最近的Git版本中,您可以为给定的远程添加多个 pushurl
。使用以下命令将两个 pushurl
s添加到您的来源
:
git remote set-url --add --push origin git://original/repo.git
git remote set-url --add --push origin git ://another/repo.git
所以当你推到 origin
,它会推送到这两个存储库。
更新1 :Git 1.8.0.1和1.8.1 (可能还有其他版本)似乎有一个错误,导致 - 添加
在首次使用时替换原始URL,因此您需要重新添加原始网址使用相同的命令。执行 git remote -v
应该会显示每个远程的当前URL。
UPDATE 2: Git维护者Junio C. Hamano解释了它是如何设计的。做 git remote set-url --add --push< remote_name> < url>
为给定的远程设备添加了 pushurl
,替代了推送的默认网址。但是,您可以为给定的遥控器添加多个 pushurl
s,然后您可以使用一个 git push $ c推送多个遥控器$ C>。您可以在下面验证此行为:
$ 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 / *
现在,如果您想使用单个命令推送到两个或更多存储库,您可以创建一个名为全部
的新远程(如 @Adam Nelson 在评论中),或者继续使用 origin
,尽管后一个名称对此目的描述较少。如果您仍想使用 origin
,请跳过以下步骤,并使用 origin
代替所有
在所有其他步骤。
因此,让我们添加一个名为 all
的新远程程序我们稍后会推荐到多个版本库:
$ git remote添加所有git://original/repo.git
$ git remote -v
all git://original/repo.git(fetch)< - 已添加
全部git://original/repo.git(推送)< - - 添加
原点git://original/repo.git(fetch)
原点git://original/repo.git(推送)
$ git config -l | grep'^ remote \.all'
remote.all.url = git://original/repo.git< - ADDED
remote.all.fetch = + refs / heads / *: refs / remotes / all / *< - ADDED
然后添加一个 pushurl
到全部
远程,指向另一个存储库:
$ 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(推送)
$ 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)。
现在我们再添加一个 pushurl
指向原始存储库:
$ git remote set-url - 添加 - 所有git://original/repo.git
$ git remote -v
所有git://original/repo.git(fetch)
所有git://另一个/repo.git(push)
all git://original/repo.git(push)< - ADDED
origin git://original/repo.git(fetch)
起源git://original/repo.git(推送)
$ 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
s。现在,一个 git push all master
会将 master
分支同时推送到 git://另一个/ repo.git
和 git://original/repo.git
。
I have two remote git repositories. origin
and github
I push my branch devel
to both repositories.
git push -u origin devel
git push -u github devel
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 ?
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
So when you push to origin
, it will push to both repositories.
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.
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/*
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.
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
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
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).
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
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
.
这篇关于Git - 将代码推送到两个遥控器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!