我有一个本地Git回购,我想把它推到一个新的远程回购(全新的回购设置在Beanstalk上,如果这很重要的话)。 我的本地回购有一些分支和标签,我想保留我的所有历史。

看起来我基本上只需要做一个git推送,但这只上传主分支。

我如何推动所有东西,以便在遥控器上获得本地回购的完整副本?


当前回答

下面的命令将推送所有的分支(包括那些你从未签出但在你的git repo中出现的分支,你可以通过git branch -a看到它们)

Git push origin '*:*'

注意:当您正在迁移版本控制时,此命令非常方便 (我的服务。e从Gitlab迁移到GitHub)

其他回答

要推送分支和标签(但不包括遥控器):

git push origin 'refs/tags/*' 'refs/heads/*'

这相当于结合了git push的——tags和——all选项,而git似乎不允许这样做。

根据@Daniel的回答,我做了:

for remote in \`git branch | grep -v master\`
do 
    git push -u origin $remote
done

我发现这些方法似乎都不适合我。放心燃烧这个死亡,但由于某种原因不能让其他选项正常工作。

预期的结果是回购“克隆”到另一个远程(即从Github到另一个提供商):

所有分支都在新的远程上创建 所有分支历史记录都在新的远程上创建 (我尝试的每个解决方案都错过了这一点) 所有标记都在新的远程上创建 源移动(给定) 非破坏性(暂停——mirror选项)

我看到的主要问题是,所有远程分支都没有在新的远程中重新创建。如果一个命令有,新的远程没有分支历史记录(即做一个git签出分支;Git日志不会显示预期的分支提交)。

我注意到git checkout -b branchname与git checkout branchname不相同(后者是我需要的)。我注意到git checkout -track branchname似乎没有拉出分支历史。

我的解决方案(基于powershell):

Function Git-FetchRemoteBranches {
$originalbranch = (git symbolic-ref HEAD).split("/")[-1]

Foreach ($entry in (git branch -r)) {

If ($entry -like "*->*") {
  $branch = $entry.split("->")[2].split("/")[1]
}
  else {$branch = $entry.split("/")[1]}

Write-Host "--Trying git checkout " -NoNewline
Write-Host "$branch" -Foreground Yellow

git checkout $branch

Remove-Variable branch -Force

""}

#Switch back to original branch, if needed
If ( ((git symbolic-ref HEAD).split("/")[-1]) -ne $originalbranch) {
"Switching back to original branch"
git checkout $originalbranch
Remove-Variable originalbranch -Force
}
}

git clone http://remoterepo
cd remoterepo
Git-FetchRemoteBranches
git remote add newremote
git push newremote --all
git push newremote --tags #Not sure if neeeded, but added for good measure

git-push的手册值得一读。结合这个网站,我在我的。git/config中写了以下内容:

[remote "origin"]
    url = …
    fetch = …
    push = :
    push = refs/tags/*

push =:表示“推送任何‘匹配的’分支(即已经存在于远程存储库并且有本地对应的分支)”,而push = refs/tags/*表示“推送所有标签”。

所以现在我只需要运行git push来推送所有匹配的分支和所有标签。

是的,这不是OP想要的(所有要推送的分支必须在远程端已经存在),但是对于那些在谷歌搜索“如何同时推送分支和标签”时发现这个问题的人来说可能是有帮助的。

如果目的地是空的,这是我发现的最简洁的方法。切换到一个空文件夹,然后:

# Note the period for cwd >>>>>>>>>>>>>>>>>>>>>>>> v
git clone --bare https://your-source-repo/repo.git .
git push --mirror https://your-destination-repo/repo.git

用https://..。对于文件:///your/repo等。