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

Git/Gerrit push repo1 to repo2 with all branches

我们在 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.

只需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/*

I am still getting this error:error: 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.

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

你说

we need to push the entire history and branches of repo1 to a new project repo2

一个新的 repo 应该是空的,因此你不应该得到这个错误。

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

您可以使用 -f

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

或将 + 添加到 refspec

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

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