Git - 将代码推送到两个遥控器[重复]

本文翻译自:Git - Pushing code to two remotes [duplicate]

This question already has an answer here: 这个问题在这里已有答案:

I have two remote git repositories. 我有两个远程git存储库。 origin and github origingithub

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 ? 无论如何我可以设置我的两个遥控器,以便我可以用一个命令将更改推送到两个存储库?


#1楼

参考:https://stackoom.com/question/xxVh/Git-将代码推送到两个遥控器-重复


#2楼

In recent versions of Git you can add multiple pushurl s for a given remote. 在最近的Git版本中,您可以为给定的遥控器添加多个pushurl Use the following to add two pushurl s to your origin : 使用以下命令将两个pushurl添加到您的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. 更新1 :Git 1.8.0.1和1.8.1(以及可能的其他版本)似乎有一个错误导致--add在您第一次使用它时替换原始URL,因此您需要使用重新添加原始URL同样的命令。 Doing git remote -v should reveal the current URLs for each remote. 执行git remote -v应该显示每个远程的当前URL。

UPDATE 2: Junio C. Hamano, the Git maintainer, explained it's how it was designed. 更新2: Git维护者Junio C. Hamano解释了它的设计方式。 Doing git remote set-url --add --push <remote_name> <url> adds a pushurl for a given remote, which overrides the default URL for pushes. 执行git remote set-url --add --push <remote_name> <url>为给定的远程添加pushurl ,它会覆盖推送的默认URL。 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 . 但是,您可以为给定的遥控器添加多个pushurl ,然后允许您使用单个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. 现在,如果您想使用单个命令推送到两个或更多存储库,您可以创建一个名为all的新远程(在评论中由@Adam Nelson建议),或者继续使用origin ,尽管后者的名称描述较少这个目的。 If you still want to use origin , skip the following step, and use origin instead of all in all other steps. 如果您仍想使用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: 然后让我们向all遥控器添加一个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 (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. 这里git remote -v显示了push的新pushurl ,所以如果你执行git push all master ,它会将master分支推送到git://another/repo.git This shows how pushurl overrides the default url (remote.all.url). 这显示了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. 你看到我们添加的两个pushurl都被保留了。 Now a single git push all master will push the master branch to both git://another/repo.git and git://original/repo.git . 现在一个git push all master会将master分支推送到git://another/repo.gitgit://original/repo.git


#3楼

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 ,它将更新两个存储库。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值