简述: 是否有一种方法可以让 git 仓库推送和拉取多个远程仓库的列表(而不是单个的“origin”)?
详述: 我经常遇到这样的情况:在多台电脑上开发应用程序,网络连接方式也不同——比如在运输中使用笔记本电脑,在某个位置使用计算机“A”,在另一个位置使用计算机“B”。此外,笔记本电脑可能只与“A”或“B”之一连接,并且有时两者都连接。
我想做的是让 git 总是从它当前可以连接到的所有电脑上 “pull” 并 “push”,这样更容易从一台机器跳转到另一台机器并继续无缝工作。
简述: 是否有一种方法可以让 git 仓库推送和拉取多个远程仓库的列表(而不是单个的“origin”)?
详述: 我经常遇到这样的情况:在多台电脑上开发应用程序,网络连接方式也不同——比如在运输中使用笔记本电脑,在某个位置使用计算机“A”,在另一个位置使用计算机“B”。此外,笔记本电脑可能只与“A”或“B”之一连接,并且有时两者都连接。
我想做的是让 git 总是从它当前可以连接到的所有电脑上 “pull” 并 “push”,这样更容易从一台机器跳转到另一台机器并继续无缝工作。
手动操作已不再必要,使用现代版本的git
即可!请参阅Malvineous的解决方案,如下所示。
以下是复制的内容:
git remote set-url origin --push --add <a remote>
git remote set-url origin --push --add <another remote>
这是我使用了相当长一段时间并且由Linus Torvalds在git邮件列表上建议的,没有带来不良后果的东西。
araqnid的解决方案是将代码推送到您的存储库中的适当方法...但是当您像我一样有多个等效的官方上游(我将一些更重要的项目克隆到私有上游、GitHub和Codaset),每天将更改推送到每个源可能会很麻烦。
长话短说,逐个使用git remote add
添加所有远程仓库...然后使用git config -e
添加一个合并的远程。假设您具有此存储库config
:
[remote "GitHub"]
url = git@github.com:elliottcable/Paws.o.git
fetch = +refs/heads/*:refs/remotes/GitHub/*
[branch "Master"]
remote = GitHub
merge = refs/heads/Master
[remote "Codaset"]
url = git@codaset.com:elliottcable/paws-o.git
fetch = +refs/heads/*:refs/remotes/Codaset/*
[remote "Paws"]
url = git@github.com:Paws/Paws.o.git
fetch = +refs/heads/*:refs/remotes/Paws/*
…来为"Paws"
和"Codaset"
创建一个合并的远程,我可以在所有这些之后添加以下内容:
[remote "Origin"]
url = git@github.com:Paws/Paws.o.git
url = git@codaset.com:elliottcable/paws-o.git
git push Origin Master
时,它会依次推送到Paws/Master
和Codaset/Master
,使生活变得更加轻松。git
命令行原生支持此功能,使用 git remote set-url ... --add
即可。 - ELLIOTTCABLEgit remote
命令配置多个远程存储库:git remote add alt alt-machine:/path/to/repo
要从所有配置的远程仓库获取并更新跟踪分支,但不合并到 HEAD
中,请执行以下操作:
git remote update
如果当前没有连接到远程之一,它将会超时或抛出错误,并继续进行下一个操作。您将需要手动从获取的存储库中合并,或者根据您希望如何组织收集更改来进行挑选。
要从alt获取主分支并将其拉入当前头部,请执行以下操作:
git pull alt master
实际上,git pull
几乎就是 git pull origin HEAD
的简写(实际上它会查看配置文件来确定这一点,但你可以明白我的意思)。
如果要推送更新,则必须手动对每个存储库执行此操作。
我认为,推送是针对中央仓库工作流程设计的。
自从 Git 1.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。
git fetch
检查。(鉴于此,如果没有--push
,我不明白git remote set-url --add
的目的是什么。) - imz -- Ivan Zakharyaschev我在我的~/.bashrc文件中添加了以下别名:
alias pushall='for i in `git remote`; do git push $i; done;'
alias pullall='for i in `git remote`; do git pull $i; done;'
git config alias.pushall '!for i in
git remote; do git push $i; done;'
。 - Ciro Santilli OurBigBook.comalias pushall='for i in \
git remote`; do echo "Pushing to " $i; git push $i; done;'` - Scott C Wilson您可以使用以下命令添加远程:
git remote add a urla
git remote add b urlb
然后要更新所有的仓库,请执行以下操作:
git remote update
git clone git@github.com:myuser/myrepo.git
$ git remote -v
origin git@github.com:myuser/myrepo.git (fetch)
origin git@github.com:myuser/myrepo.git (push)
origin
:git remote set-url --add origin git@bitbucket.org:myuser/myrepo.git
push
的列表是否都列出:$ git remote -v
origin git@github.com:myuser/myrepo.git (fetch)
origin git@github.com:myuser/myrepo.git (push)
origin git@bitbucket.org:myuser/myrepo.git (push)
$ git config --local --get-regexp ^remote\..+\.url$
remote.origin.url git@github.com:myuser/myrepo.git
remote.origin.url git@bitbucket.org:myuser/myrepo.git
git push
删除远程分支:
要删除一个远程分支:
git remote set-url --delete origin git@bitbucket.org:myuser/myrepo.git
这里是我的示例,它在.gitconfig
别名部分内包含了bash脚本。
[alias]
pushall = "!f(){ for i in `git remote`; do git push $i; done; };f"
2023年更新
如果您想同时推送多个远程仓库,您可能已经足够了解Git。在git中,我们将远程仓库称为远程仓库。将更改推送到远程仓库是正常开发周期的一部分。
有时候,您可能需要将更改推送到多个远程仓库,比如GitHub、bitbucket等。为此,您可以按照以下说明操作:
您可以使用以下命令列出所有可用的远程仓库。
git remote -v
假设您尚未配置任何其他远程(remote)。您可以通过使用git remote进行配置。
git remote add remote_name remote_url
示例:
git remote add github https//github.com/path/to/repo
git remote add all <remote URL>
git remote set-url --add all <another remote URL>
你可以使用git remote -v
交叉检查添加的新远程。
(或者)
git config
命令用于配置git参数。它将根据给定的输入编辑.git/config
文件。
git config --add remote.all.url https//domain.com/repo.git
git config --add remote.all.url ssh://user@host/repos/repo.git
注意:没有--add
选项,该命令将替换现有的远程URL。您可以在.git/config
中验证更新后的配置。
(或者)
如果您了解配置格式,请编辑文件.git/config
以添加远程和多个远程URL。
现在,您可以通过引用具有多个远程URL分配的远程名称来同时推送多个远程操作。
git push all master
你可以使用正式的bash语法而不用将它们分组,始终向多个远程存储库推送。
git push server master && git push github master
git push origin "branchName"
时,它会通过每个URL推送。不确定是否有更简单的方法来完成此操作,但这对我来说可以同时将代码推送到Github源代码和My.visualStudio源代码。[remote "origin"]
url = "Main Repo URL"
fetch = +refs/heads/*:refs/remotes/origin/*
pushurl = "repo1 URL"
pushurl = "reop2 URl"
我想要在VSO/TFS上工作,然后在就绪时向GitHub公开发布。最初的存储库是在私有VSO中创建的。当需要添加到GitHub时,我执行了以下操作:
git remote add mygithubrepo https://github.com/jhealy/kinect2.git
git push -f mygithubrepo master
非常顺利的工作...
作为一种理智检查方式,可以通过执行“git remote -v”来列出与项目相关联的存储库。
C:\dev\kinect\vso-repo-k2work\FaceNSkinWPF>git remote -v
githubrepo https://github.com/jhealy/kinect2.git (fetch)
githubrepo https://github.com/jhealy/kinect2.git (push)
origin https://devfish.visualstudio.com/DefaultCollection/_git/Kinect2Work (fetch)
origin https://devfish.visualstudio.com/DefaultCollection/_git/Kinect2Work (push)
这是一种简单的方法,对我有用...希望能帮到其他人。
git push -f
,例如git push
失败或确切知道自己在做什么,是一个不好的想法并且可能会造成伤害。这也没有回答问题,但是如何添加第二个远程仓库。 - Kalle Richter
git
功能,包含在malvineous的下面的答案中,被认可的答案是不正确的。 - ELLIOTTCABLE