Git推分支从一个遥控器到另一个遥控器?

时间:2021-05-06 16:24:17

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?

现在我希望将所有远程分支从korg推送到rorg远程。我怎么做?

Preferably without making a local branch for each first, if that is avoidable.

优选地,如果可以避免,则不为每个第一分支建立本地分支。

5 个解决方案

#1


33  

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

快速测试制作一些临时存储库显示您可以构建一个可以执行此操作的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

So origin/BRANCHNAME:refs/heads/BRANCHNAME

所以origin / BRANCHNAME:refs / heads / BRANCHNAME

Checking in my rorg remote:

检查我的rorg遥控器:

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

#2


164  

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:

它将我所有的远程分支从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

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

然后你可以为标签refs做同样的推动:

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

#3


12  

To complement patthoyt's answer, here's a short shell script that pushes all the branches from one remote to another:

为了补充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

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

总而言之,对于源远程上的每个远程分支(不包括像HEAD这样的“指针”分支),将该ref推送到目标远程。 ($ {a // $ SRC_REMOTE \ /}位从分支名称中删除源远程名称,即origin / master成为master。)

#4


4  

This works in Zsh

这适用于Zsh

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

#5


0  

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".

只需将origin1更改为源远程,将origin2更改为目标远程。将其复制到“remoteBranchCloner.sh”并使用“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:

对于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.

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

git config [--global] credential.helper wincred

For linux

对于Linux

git config [--global] credential.helper cache

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

其中[--global]表示可选择添加--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'

存储为.sh文件并使用“sh filename.sh”运行将设置所有上游以跟踪远程'remotename'

#1


33  

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

快速测试制作一些临时存储库显示您可以构建一个可以执行此操作的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

So origin/BRANCHNAME:refs/heads/BRANCHNAME

所以origin / BRANCHNAME:refs / heads / BRANCHNAME

Checking in my rorg remote:

检查我的rorg遥控器:

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

#2


164  

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:

它将我所有的远程分支从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

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

然后你可以为标签refs做同样的推动:

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

#3


12  

To complement patthoyt's answer, here's a short shell script that pushes all the branches from one remote to another:

为了补充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

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

总而言之,对于源远程上的每个远程分支(不包括像HEAD这样的“指针”分支),将该ref推送到目标远程。 ($ {a // $ SRC_REMOTE \ /}位从分支名称中删除源远程名称,即origin / master成为master。)

#4


4  

This works in Zsh

这适用于Zsh

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

#5


0  

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".

只需将origin1更改为源远程,将origin2更改为目标远程。将其复制到“remoteBranchCloner.sh”并使用“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:

对于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.

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

git config [--global] credential.helper wincred

For linux

对于Linux

git config [--global] credential.helper cache

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

其中[--global]表示可选择添加--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'

存储为.sh文件并使用“sh filename.sh”运行将设置所有上游以跟踪远程'remotename'