2011-04-11 78 views
38

我想在同一行中执行git push origingit push my_other_remote。可能?Git:在一个命令中推送到两个回购

+0

我不认为你可以使用标准命令做到这一点,但你可以写一个'git-multipush'或其他东西并使用它。 – 2011-04-11 11:26:02

+0

[从多个远程位置拉出/推送]可能重复(http://stackoverflow.com/questions/849308/pull-push-from-multiple-remote-locations) – techraf 2016-08-20 00:30:33

回答

84

您可以通过为您的origin远程设备添加额外的推送URL来获得相同的效果。例如,如果您现有的遥控器的网址如下:

$ git remote -v 
origin [email protected]:something.git (fetch) 
origin [email protected]:something.git (push) 
my_other_remote git://somewhere/something.git (fetch) 
my_other_remote git://somewhere/something.git (push) 

你可以这样做:

git remote set-url --add --push origin git://somewhere/something.git 

然后,git push origin将推动两个库。但是,为避免混淆,您可能需要为此设置一个名为both的新远程设备。例如:

git remote add both [email protected]:something.git 
git remote set-url --add --push both [email protected]:something.git 
git remote set-url --add --push both git://somewhere/something.git 

...然后:

git push both 

...会尽量推到两个库。

+0

这是否也会更新'remote/origin'和'remote/my_other_remote'跟踪分支? – 2011-04-11 12:02:27

+0

@PaŭloEbermann:我的(非常简短的)测试表明'refs/remotes/origin/master'和'refs/remotes/my_other_remote/master'将在两者的URL作为推送URL添加的情况下被更新到'origin'并且你做'git push origin'。 – 2011-04-11 13:21:55

+0

这看起来可能像我正在寻找的,但我仍然有点混淆......如果我想要将我的应用程序提交给Heroku,还有BitBucket,该如何继续?首先,我必须创建回购协议到Heroku,然后以同样的方式在BitBucket上创建回购,或者正确的方法是什么? – user984621 2012-03-04 10:33:12

10

你可以把在.git/config文件中的以下内容:

[remote "both"] 
    url = url/to/first/remote 
    url = url/to/other/remote 

现在可以使用git push both推到这两个URL。

如果你也想获取从他们(sync用有用),你可能在你的.git/config文件中添加以下行:

[remotes] 
    both = origin, other 

现在你也可以运行git fetch both

+6

是您提供的基本上与Mark一样的解决方案(除非您通过手动编辑来完成),还是还有其他区别? – 2015-04-29 15:22:15

+2

没有区别。 – silvio 2016-03-08 13:44:38