Git / Gerrit将repo1推送到repo2与所有分支

时间:2015-01-08 10:23:56

标签: git

我们在Gerrit有一个项目(例如repo1),我们需要将repo1的整个历史和分支推送到新的项目repo2。以下是目前执行的一些步骤

 git clone ssh://$USER\@$HOST:29418/repo1 
 for remote in `git branch -r | grep -v master`; do git branch --track $remote
 git remote remove origin
 git remote add origin ssh://${USER}\@${HOST}:29418/repo2
 and then ..
 cd /var/tmp/${project} && git push origin master && git push origin --all && git push       
 origin --tags

但它没有要求我拉动变化。一旦我提取了更改,就会错误地说我没有任何远程定位设置。在这方面我现在都很困惑。如何将具有历史记录和多个分支的项目推送到新项目。有什么建议吗?

编辑1:

在尝试了Rene的建议后,我收到了这个错误:

failed to push some refs to 'ssh://$USER@$HOST:29418/repo2'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first merge the remote changes (e.g.,
hint: 'git pull') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

1 个答案:

答案 0 :(得分:1)

您只需要3个步骤

git clone ssh://$USER\@$HOST:29418/repo1

只需将repo2添加为另一个远程

即可
git remote add repo2 ssh://${USER}\@${HOST}:29418/repo2

使用refspec,您不需要创建本地分支

git push repo2 refs/remotes/origin/*:refs/heads/* refs/tags/*:refs/tags/*

修改

您可以在github上使用我的GitDirStat存储库进行试用,例如

BASE_DIR=`pwd`
git clone https://github.com/link-intersystems/GitDirStat.git
git init --bare GitDirStat-repo2
cd GitDirStat
git remote add repo2 file://$BASE_DIR/GitDirStat-repo2
git push repo2 refs/remotes/origin/*:refs/heads/* refs/tags/*:refs/tags/*
  

我仍然收到此错误:错误:无法将某些引用推送到'ssh:// $ USER @ $ HOST:29418 / repo2'提示:更新被拒绝,因为遥控器包含您提示的工作:没有本地。

远程存储库已经包含您要推送的分支,而git无法更新它们进行快进。所以你推的分支与远程分支不一致。

你说那个

  

我们需要将repo1的整个历史记录和分支推送到新项目repo2

新的仓库应为空,因此您不应该收到此错误。

如果repo2 NOT 包含任何重要更改,您可以执行强制推送。我建议首先制作备份克隆

您可以使用-f

进行强制推送
git push -f ...

或将+添加到refspec

+refs/remotes/origin/*:refs/heads/*

这会强制更新ref,并且refs指向的提交将丢失。