git Git将分支从一个远程推送到另一个?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7818927/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Git push branch from one remote to another?
提问by Bjarke Freund-Hansen
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.
如果可以避免,最好不要先为每个分支创建本地分支。
采纳答案by patthoyts
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
回答by radistao
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:
然后你可以对tags
refs进行同样的推送:
git push rorg 'refs/tags/*:refs/tags/*'
回答by blahdiblah
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 这样的“指针”分支),将该引用推送到目标远程。(该${a//$SRC_REMOTE\/}
位从分支名称中剥离了源远程名称,即origin/master
变为master
。)
回答by zhudongfang
This works in Zsh
这适用于 Zsh
git push rorg 'refs/remotes/korg/*:refs/heads/*'
回答by Kurt Vangraefschepe
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:
由于HEAD
在上一个答案中创建了额外的分支,我发现最干净的方法是克隆到一个裸存储库,然后将所有分支推送到远程,如下所示:
git clone --bare <from-repository>
cd <from-repo-dir>
git push --set-upstream <to-repository> --all
git push --set-upstream <to-repository> --tags
回答by csga5000
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 betterway, 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.
注意:此脚本适用于 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”