Git 将分支从一个远程推送到另一个远程?

发布于 2024-12-10 20:23:32 字数 522 浏览 0 评论 0原文

我设置了以下遥控器:

$ 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

孤独陪着我 2024-12-17 20:23:32

我找到了这个:

git push rorg 'refs/remotes/korg/*:refs/heads/*'

它将我所有的远程分支从 korg 推送到 rorg (即使没有分支的本地副本)。请参阅下面的输出:

Counting objects: 293, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (67/67), done.
Writing objects: 100% (176/176), 48.32 KiB, done.
Total 176 (delta 105), reused 168 (delta 97)
remote: Resolving deltas:  11% (12/105)
To <<MY_REPOSITORY_URL>>
 * [new branch]      korg/gingerbread-> gingerbread
 * [new branch]      korg/gingerbread-release -> gingerbread-release
 * [new branch]      korg/honeycomb-> honeycomb
 * [new branch]      korg/HEAD -> HEAD
 * [new branch]      korg/honeycomb-mr1-release-> honeycomb-mr1-release
 * [new branch]      korg/master -> master

然后您可以对 tags refs 进行相同的推送:

git push rorg 'refs/tags/*:refs/tags/*'

I've found this one:

git push rorg 'refs/remotes/korg/*:refs/heads/*'

And it pushed all my remote branches from korg to rorg (even without local copies of the branches). See the output below:

Counting objects: 293, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (67/67), done.
Writing objects: 100% (176/176), 48.32 KiB, done.
Total 176 (delta 105), reused 168 (delta 97)
remote: Resolving deltas:  11% (12/105)
To <<MY_REPOSITORY_URL>>
 * [new branch]      korg/gingerbread-> gingerbread
 * [new branch]      korg/gingerbread-release -> gingerbread-release
 * [new branch]      korg/honeycomb-> honeycomb
 * [new branch]      korg/HEAD -> HEAD
 * [new branch]      korg/honeycomb-mr1-release-> honeycomb-mr1-release
 * [new branch]      korg/master -> master

And then you can make the same push for tags refs:

git push rorg 'refs/tags/*:refs/tags/*'
隔岸观火 2024-12-17 20:23:32

制作一些临时存储库的快速测试表明您可以构建一个可以执行此操作的 refspec:

$ git push rorg origin/one:refs/heads/one
Counting objects: 5, done.
Writing objects: 100% (3/3), 240 bytes, done.
Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
To /tmp/rorg
 * [new branch]      origin/one -> one

因此 origin/BRANCHNAME:refs/heads/BRANCHNAME

检查我的 rorg 远程:

pat@host /tmp/rorg (BARE:master)
$ git graph --all
* 5750bca (HEAD, master) c
| * 13fd55a (one) b
|/
* 822e0de a

A quick test making some temporary repositories shows you can construct a refspec that can do this:

$ git push rorg origin/one:refs/heads/one
Counting objects: 5, done.
Writing objects: 100% (3/3), 240 bytes, done.
Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
To /tmp/rorg
 * [new branch]      origin/one -> one

So origin/BRANCHNAME:refs/heads/BRANCHNAME

Checking in my rorg remote:

pat@host /tmp/rorg (BARE:master)
$ git graph --all
* 5750bca (HEAD, master) c
| * 13fd55a (one) b
|/
* 822e0de a
亽野灬性zι浪 2024-12-17 20:23:32

为了补充 patthoyt 的答案,这里有一个简短的 shell 脚本,它将所有分支从一个远程推送到另一个远程:

SRC_REMOTE=korg
DST_REMOTE=rorg
for a in $(git branch --list --remote "$SRC_REMOTE/*" | grep -v --regexp='->')
  do git push "$DST_REMOTE" "$a:refs/heads/${a//$SRC_REMOTE\/}"
done

总结一下,对于每个源远程上的远程分支(不包括“指针”分支,如 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:

SRC_REMOTE=korg
DST_REMOTE=rorg
for a in $(git branch --list --remote "$SRC_REMOTE/*" | grep -v --regexp='->')
  do git push "$DST_REMOTE" "$a:refs/heads/${a//$SRC_REMOTE\/}"
done

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 becomes master.)

つ可否回来 2024-12-17 20:23:32

这在 Zsh 中有效。

注意,在某些情况下,单引号对于防止意外的参数扩展是必要的。

git push rorg 'refs/remotes/korg/*:refs/heads/*'

This works in Zsh

Notice the single quote is necessary to prevent unexpected parameter expansion in some cases.

git push rorg 'refs/remotes/korg/*:refs/heads/*'
淡淡離愁欲言轉身 2024-12-17 20:23:32

对于我建议您运行的任何脚本,明智的做法是存储或提交所有更改。

我需要将多个分支从一个远程推送到另一个远程。这些答案要求本地分支先前存在,

SRC_R=origin1
DEST_R=origin2
for cbranch in $(git branch -r | grep $SRC_R | cut -d '/' -f2,3,4,5 | cut -d ' ' -f1)
do
    git checkout $cbranch
    git push $DEST_R $cbranch
done

只需将 origin1 更改为源远程,将 origin2 更改为目标远程。
将其复制到“remoteBranchCloner.sh”并使用“sh callBranchCloner.sh”调用它。

可能有一种更好的方法,不需要多次推送。

如果您使用我的代码,您可能需要使用凭据缓存,否则您必须多次输入您的凭据。

对于 Windows

注意:此脚本适用于 Linux。如果您在“git bash”中运行它,该脚本将起作用,但如果没有安装特殊的东西,则无法从本机控制台运行它。

git config [--global] credential.helper wincred

对于 Linux

git config [--global] credential.helper cache

其中 [--global] 表示可选添加 --global

如果您想将所有分支的远程跟踪设置为新的远程:

DEST_R=remotename
for cbranch in `git branch`
do
    git checkout $cbranch
    git branch -u guru/$cbranch
done

存储为 .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

SRC_R=origin1
DEST_R=origin2
for cbranch in $(git branch -r | grep $SRC_R | cut -d '/' -f2,3,4,5 | cut -d ' ' -f1)
do
    git checkout $cbranch
    git push $DEST_R $cbranch
done

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.

git config [--global] credential.helper wincred

For linux

git config [--global] credential.helper cache

Where [--global] means optionally add --global

If you would like to set remote tracking for all branches to a new remote:

DEST_R=remotename
for cbranch in `git branch`
do
    git checkout $cbranch
    git branch -u guru/$cbranch
done

Stored as a .sh file and ran with "sh filename.sh" will set all upstreams to track remote 'remotename'

橘香 2024-12-17 20:23:32

由于在之前的 答案,我发现执行此操作的最简洁方法是克隆到裸存储库,然后将所有分支推送到远程,如下所示:

git clone --bare <from-repository>
cd <from-repo-dir>
git push --set-upstream <to-repository> --all
git push --set-upstream <to-repository> --tags

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:

git clone --bare <from-repository>
cd <from-repo-dir>
git push --set-upstream <to-repository> --all
git push --set-upstream <to-repository> --tags
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文