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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 15:15:23  来源:igfitidea点击:

Git - Pushing code to two remotes

gitgithub

提问by yasith

I have tworemote git repositories. originand github

我有两个远程 git 存储库。origingithub

I push my branch develto both repositories.

我将我的分支推devel送到两个存储库。

git push -u origin devel
git push -u github devel

But then, when I do. git pushIt 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 pushurls for a given remote. Use the following to add two pushurls to your origin:

在最新版本的 Git 中,您可以pushurl为给定的远程添加多个s。使用以下命令将两个pushurls添加到您的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 --addto 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 -vshould 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 pushurlfor a given remote, which overridesthe default URL for pushes. However, you may add multiple pushurls 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 origininstead of allin all other steps.

现在,如果您想使用单个命令推送到两个或多个存储库,您可以创建一个名为的新远程all(如@Adam Nelson在评论中所建议的那样),或者继续使用origin,尽管后者名称对此目的的描述性较差. 如果您仍想使用origin,请跳过以下步骤,并在所有其他步骤中使用origin代替all

So let's add a new remote called allthat 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 pushurlto the allremote, pointing to another repository:

然后让我们pushurlall远程添加一个,指向另一个存储库:

$ 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 -vshows the new pushurlfor push, so if you do git push all master, it will push the masterbranch to git://another/repo.gitonly. This shows how pushurloverrides 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 pushurlpointing 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 pushurls we added are kept. Now a single git push all masterwill push the masterbranch to both git://another/repo.gitand git://original/repo.git.

您会看到pushurl我们添加的两个s 都保留了。现在,单个git push all mastermaster分支推送到两者git://another/repo.gitgit://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,它将更新两个存储库。