做空者:有没有一种方法可以让git回购推送到远程回购列表(而不是一个单一的“来源”)中并从中提取?
长篇大论:当我在多台电脑上开发一个应用程序时,我经常会遇到这样的情况,这些电脑具有不同的连接能力——比如,一台笔记本电脑在运输途中,一台电脑“a”在某个位置,另一台电脑则“B”在另一台位置。此外,笔记本电脑可能只与“A”或“B”连接,有时两者都连接。
我希望git始终“拉”到它当前可以连接的所有计算机,这样从一台机器跳到另一台机器,继续无缝工作就更容易了。
做空者:有没有一种方法可以让git回购推送到远程回购列表(而不是一个单一的“来源”)中并从中提取?
长篇大论:当我在多台电脑上开发一个应用程序时,我经常会遇到这样的情况,这些电脑具有不同的连接能力——比如,一台笔记本电脑在运输途中,一台电脑“a”在某个位置,另一台电脑则“B”在另一台位置。此外,笔记本电脑可能只与“A”或“B”连接,有时两者都连接。
我希望git始终“拉”到它当前可以连接的所有计算机,这样从一台机器跳到另一台机器,继续无缝工作就更容易了。
当前回答
在Git的最新版本中,您可以为给定的远程添加多个pushurl。使用以下方法将两个推送URL添加到源代码中:
git remote set-url --add --push origin git://original/repo.git
git remote set-url --add --push origin git://another/repo.git
因此,当您推送到源时,它将推送到两个存储库。
更新1:Git 1.8.0.1和1.8.1(以及可能的其他版本)似乎有一个bug,导致在第一次使用时添加以替换原始URL,因此需要使用相同的命令重新添加原始URL。执行gitremote-v应该显示每个远程的当前URL。
更新2:Git维护人员Junio C.Hamano解释了它是如何设计的。执行git remote set url--add--push<remote_name>将为给定的远程添加一个pushurl,它将覆盖push的默认url。然而,您可以为一个给定的远程添加多个pushurl,这样您就可以使用一个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/*
现在,如果您想使用一个命令推送到两个或多个存储库,您可以创建一个新的名为all的远程(正如@Adam Nelson在评论中所建议的那样),或者继续使用源,尽管后者的名称对这个目的的描述较少。如果仍要使用原点,请跳过以下步骤,在所有其他步骤中使用原点而不是全部。
因此,让我们添加一个名为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
然后,让我们将一个pushurl添加到all remote,指向另一个存储库:
$ 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
这里gitremote-v显示了push的新pushurl,因此如果您执行git push all master,它会将master分支推到git://another/repo.git只有这显示了pushurl如何覆盖默认url(remote.all.url)。
现在,让我们添加另一个指向原始存储库的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
您可以看到我们添加的两个推送URL都保持不变。现在,一个单位数的push-all-master将把master分支推到两个git://another/repo.git和git://original/repo.git.
重要提示:如果您的遥控器有不同的规则(钩子)来接受/拒绝推送,则一个遥控器可能会接受推送,而另一个则不会。因此,如果您希望它们具有完全相同的历史记录,则需要在本地修复提交,以使它们能够被两个远程设备接受,然后再次推送,否则您可能会陷入只能通过重写历史记录(使用push-f)来修复它的情况,这可能会给已经从repo中删除以前更改的人带来问题。
其他回答
添加新远程
git remote add upstream https://github.com/example-org/example-repo.git
git remote -vv
从多个位置提取
git fetch --all
推送至位置
git push -u upstream/dev
自git1.8(2012年10月)以来,您可以通过命令行执行此操作:
git remote set-url origin --push --add user1@repo1
git remote set-url origin --push --add user2@repo2
git remote -v
然后git push将推到user1@repo1,然后推到user2@repo2.
我冒昧地扩大了诺娜·乌比兹的答案;只需将此添加到~/.bashrc:
git-pullall () { for RMT in $(git remote); do git pull -v $RMT $1; done; }
alias git-pullall=git-pullall
git-pushall () { for RMT in $(git remote); do git push -v $RMT $1; done; }
alias git-pushall=git-pushall
用法:
git-pullall master
git-pushall master ## or
git-pushall
如果您没有为git pulll提供任何分支参数,那么来自非默认远程的pull将失败;保持这种行为不变,因为它类似于git。
你需要一个脚本来循环它们。Git不提供“推送全部”。理论上可以在多个线程中进行推送,但本地方法不可用。
提取甚至更复杂,我建议线性地进行。
我认为你最好的答案是有一台机器,如果有可能的话,每个人都可以推/拉它。
下面是我在.gitconfig别名部分中使用bash脚本的示例
[alias]
pushall = "!f(){ for i in `git remote`; do git push $i; done; };f"