Git 将分支从一个远程推送到另一个远程?
我设置了以下遥控器:
$ git remote
korg
rorg
以及以下分支:
$ git branch -a
* (no branch)
remotes/korg/gingerbread
remotes/korg/gingerbread-release
remotes/korg/honeycomb
remotes/korg/honeycomb-mr1-release
remotes/korg/master
remotes/m/android-2.3.3_r1 -> refs/tags/android-2.3.3_r1a
remotes/m/gingerbread -> korg/gingerbread
现在我希望将所有远程分支从 korg
推送到 rorg
遥控器。我该怎么做?
如果可以避免的话,最好不要先为每个分支建立一个本地分支。
I have the following remotes set up:
$ git remote
korg
rorg
And the following branches:
$ git branch -a
* (no branch)
remotes/korg/gingerbread
remotes/korg/gingerbread-release
remotes/korg/honeycomb
remotes/korg/honeycomb-mr1-release
remotes/korg/master
remotes/m/android-2.3.3_r1 -> refs/tags/android-2.3.3_r1a
remotes/m/gingerbread -> korg/gingerbread
Now I wish to push all the remote branches from korg
to the rorg
remote. How do I do that?
Preferably without making a local branch for each first, if that is avoidable.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我找到了这个:
它将我所有的远程分支从 korg 推送到 rorg (即使没有分支的本地副本)。请参阅下面的输出:
然后您可以对
tags
refs 进行相同的推送:I've found this one:
And it pushed all my remote branches from korg to rorg (even without local copies of the branches). See the output below:
And then you can make the same push for
tags
refs:制作一些临时存储库的快速测试表明您可以构建一个可以执行此操作的 refspec:
因此 origin/BRANCHNAME:refs/heads/BRANCHNAME
检查我的
rorg
远程:A quick test making some temporary repositories shows you can construct a refspec that can do this:
So origin/BRANCHNAME:refs/heads/BRANCHNAME
Checking in my
rorg
remote:为了补充 patthoyt 的答案,这里有一个简短的 shell 脚本,它将所有分支从一个远程推送到另一个远程:
总结一下,对于每个源远程上的远程分支(不包括“指针”分支,如 HEAD),将该引用推送到目标远程。 (
${a//$SRC_REMOTE\/}
位从分支名称中剥离源远程名称,即origin/master
变为master
.)To complement patthoyt's answer, here's a short shell script that pushes all the branches from one remote to another:
To summarize, for each remote branch on the source remote (excluding "pointer" branches like HEAD), push that ref to the destination remote. (The
${a//$SRC_REMOTE\/}
bit strips the source remote name from the branch name, i.e.,origin/master
becomesmaster
.)这在 Zsh 中有效。
注意,在某些情况下,单引号对于防止意外的参数扩展是必要的。
This works in Zsh
Notice the single quote is necessary to prevent unexpected parameter expansion in some cases.
对于我建议您运行的任何脚本,明智的做法是存储或提交所有更改。
我需要将多个分支从一个远程推送到另一个远程。这些答案要求本地分支先前存在,
只需将 origin1 更改为源远程,将 origin2 更改为目标远程。
将其复制到“remoteBranchCloner.sh”并使用“sh callBranchCloner.sh”调用它。
可能有一种更好的方法,不需要多次推送。
如果您使用我的代码,您可能需要使用凭据缓存,否则您必须多次输入您的凭据。
对于 Windows:
注意:此脚本适用于 Linux。如果您在“git bash”中运行它,该脚本将起作用,但如果没有安装特殊的东西,则无法从本机控制台运行它。
对于 Linux
其中 [--global] 表示可选添加 --global
如果您想将所有分支的远程跟踪设置为新的远程:
存储为 .sh 文件并使用“sh filename.sh”运行将设置所有上游来跟踪远程“remotename”
For any script I sugges you run, it would be wise to stash or commit all your changes.
I needed to push several branches from one remote to another. These answers required that the local branches previously existed
Just change origin1 to the source remote, and origin2 to the destination remote.
Copy this into "remoteBranchCloner.sh" and call it using "sh callBranchCloner.sh".
There may be a better way, that doesn't do several pushes.
If you use my code you probably want to use credential caching, otherwise you have to type your credentials serveral times.
For windows:
Note: This script is for linux. If you run it in "git bash" the script will work, but you can't run it from the native console without having installed something special.
For linux
Where [--global] means optionally add --global
If you would like to set remote tracking for all branches to a new remote:
Stored as a .sh file and ran with "sh filename.sh" will set all upstreams to track remote 'remotename'
由于在之前的 答案,我发现执行此操作的最简洁方法是克隆到裸存储库,然后将所有分支推送到远程,如下所示:
Due to the extra
HEAD
branch being created in the previous answer, the cleanest way I found to do this is to clone into a bare repository and then push all branches to the remote as follows: