将 svn 转换为 git,如何让分支在 svn 存储库中不只是远程?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1584675/
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
Converting svn to git, how to get the branches not to be just remote in the svn repo?
提问by J?rgen Lundberg
I'm quite new to git and I'm trying to move a svn repository to git. I followed the guide below so now I have a git repo on my server
http://pauldowman.com/2008/07/26/how-to-convert-from-subversion-to-git/
我对 git 很陌生,我正在尝试将 svn 存储库移动到 git。我遵循了下面的指南,所以现在我的服务器上有一个 git repo
http://pauldowman.com/2008/07/26/how-to-convert-from-subversion-to-git/
So, if I do "git branch" git replies "* master" and if I do "git branch -r" i get a list of all the branches in the svn repository.
因此,如果我执行“git branch”,git 回复“* master”,如果我执行“git branch -r”,我会得到 svn 存储库中所有分支的列表。
My last svn-checkins have been in one of the branches, but when I did the "git svn clone"-stuff my commits in that branch (they have not been merged into the trunk yet) are visible in my (git) master branch. What am I missing here?
我最后一次 svn-checkins 已经在其中一个分支中,但是当我执行“git svn clone”-stuff 时,我在该分支中的提交(它们尚未合并到主干中)在我的 (git) 主分支中可见. 我在这里错过了什么?
Also, if I on my development machine do "git clone " I get the files alright. But if I do "git branch -r" I can only see the master branch and not the remote branches".
另外,如果我在我的开发机器上执行“git clone”,我会得到文件。但是如果我执行“git branch -r”,我只能看到主分支而不是远程分支”。
Since we're getting rid of the svn-repo all together I would like to have all the svn branches in the git-repo so that they can be accessed from the developer clients.
由于我们一起摆脱了 svn-repo,我希望在 git-repo 中包含所有 svn 分支,以便可以从开发人员客户端访问它们。
Again, I'm not a total git newbie but not far from it. So if there is something fundamental I'm missing here please tell me.
再说一次,我不是一个完全的 git 新手,但离它不远。所以如果我在这里遗漏了一些基本的东西,请告诉我。
Update
After doing some RTFM (man git-svn) I solved the first problem with branch stuff present in the master branch
更新
在做了一些 RTFM (man git-svn) 之后,我解决了 master 分支中存在的分支内容的第一个问题
reset --hard remotes/trunk
重置 --hard 遥控器/中继
Now the trunk and the master branch are the same. Now, next is to figure out how to get the branches from the development clients.
现在主干和主分支是一样的。现在,接下来是弄清楚如何从开发客户端获取分支。
Update 2
I got it working by combining the url above and the url that Scott pointed to. So, from the beginning.
更新 2
我通过组合上面的 url 和 Scott 指向的 url 让它工作。所以,从一开始。
I first created an empty repository on the server, they are kept in /usr/local/git-repos on our server:
我首先在服务器上创建了一个空存储库,它们保存在我们服务器上的 /usr/local/git-repos 中:
server> cd /usr/local/git-repos
server> mkdir my_project.git
server> cd my_project.git
server> git init
Then i cloned the svn-repository to my dev-machine (note, that on our svn server the "branches" dir is called "branch" witoutthe 's'):
然后我将 svn-repository 克隆到我的开发机器上(注意,在我们的 svn 服务器上,“分支”目录被称为“分支”而没有“s”):
dev> git svn clone http://<svn.server>/my_project --no-metadata -A authors.txt -t tags -b branch -T trunk my_project
Then some clean-up to get the tags and branches in order:
然后进行一些清理以按顺序获取标签和分支:
dev> cp -Rf .git/refs/remotes/tags/* .git/refs/tags/
dev> rm -Rf .git/refs/remotes/tags
dev> cp -Rf .git/refs/remotes/* .git/refs/heads/
dev> rm -Rf .git/refs/remotes
Now add my server as a remote repository:
现在将我的服务器添加为远程存储库:
dev> git remote add origin jorgen@<our_server>:/usr/local/git-repos/my_project.git
Finally, push all branches and tags up to the server:
最后,将所有分支和标签推送到服务器:
dev> git push origin --all
Phew, now there you have it, now I can get rid of that svn-repo.
呼,现在你有了它,现在我可以摆脱那个 svn-repo。
Update 3
Checkout ebneters postbelow for an easier way of doing it...
更新 3
Checkout ebneters 在下面发布,以便更轻松地完成此操作...
采纳答案by Scott Chacon
There is a fairly detailed explanation on how to do a pretty good SVN import that explains how to convert the branches properly here:
关于如何进行非常好的 SVN 导入,有一个相当详细的解释,解释了如何在此处正确转换分支:
https://git-scm.com/book/en/v1/Git-and-Other-Systems-Migrating-to-Git
https://git-scm.com/book/en/v1/Git-and-Other-Systems-Migrating-to-Git
The short answer is to run this:
简短的回答是运行这个:
$ cp -Rf .git/refs/remotes/* .git/refs/heads/
$ rm -Rf .git/refs/remotes
Hope that's helpful.
希望这有帮助。
回答by ebneter
Another good way to do this, just for the record, is to use svn2git— I'm in the middle of converting several rather large repositories and it has been a godsend. It automates all of the steps needed to take care of branches and convert svn tags to real git tags.
另一个好方法,只是为了记录,是使用svn2git— 我正在转换几个相当大的存储库,这是天赐之物。它自动执行处理分支和将 svn 标签转换为真正的 git 标签所需的所有步骤。
回答by David
Scott's solution didn't work for me. I suspect something may have changed in a recent version of git-svn since he posted that (and since the linked book was written), as it seems to aggressively garbage-collect as soon as the clone is complete. But this is just a guess as to why it didn't work. I'm using git 1.6.5.6.
斯科特的解决方案对我不起作用。我怀疑 git-svn 的最新版本可能发生了变化,因为他发布了那个(以及自链接的书被编写以来),因为一旦克隆完成,它似乎就会积极地进行垃圾收集。但这只是关于为什么它不起作用的猜测。我正在使用 git 1.6.5.6。
Specifically, my .git/refs/remotes directory was completely empty except for a tags directory, which was also empty. So there is nothing I can copy to make it right.
具体来说,我的 .git/refs/remotes 目录是完全空的,除了 tags 目录也是空的。所以我无法复制以使其正确。
After some poking around, I was able to fix this by checking the file .git/packed-refs and doing search-and-replace on the following (in this order):
经过一番摸索,我能够通过检查文件 .git/packed-refs 并按以下顺序执行搜索和替换来解决此问题(按此顺序):
refs/remotes/tags => refs/tags
refs/remotes => refs/heads
If your editor is vim, you can do it with these two commands:
如果您的编辑器是 vim,则可以使用以下两个命令来完成:
:%s/refs\/remotes\/tags/refs\/tags/g
:%s/refs\/remotes/refs\/heads/g
svn2git 1.3.1 also did not produce a usable result for me (did not import any commits after a certain point several months ago, and branches all showed the same commits). For now I have given up on svn2git and have had the most success using git-svn combined with the above.
svn2git 1.3.1 也没有为我产生可用的结果(几个月前的某个时间点之后没有导入任何提交,并且分支都显示相同的提交)。现在我已经放弃了 svn2git 并且使用 git-svn 与上面的结合取得了最大的成功。
Wishful thinking: sure would be nice if git-svn simply added a command like 'abandon' or 'migrate' that would automate this process in a future-proof way.
一厢情愿的想法:如果 git-svn 简单地添加一个像“放弃”或“迁移”这样的命令,以一种面向未来的方式自动化这个过程,那肯定会很好。
回答by radovan
I have migrated 2 svn repos to git (git version 1.7.0.4) following Scott's recipe, a smaller one and a larger one. The smaller one behaved as described by Scott in the book chapter. The larger one required David's solution. Another thing is that
我已经按照 Scott 的食谱将 2 个 svn repos 迁移到 git(git 版本 1.7.0.4),一个较小的一个,一个较大的。较小的行为如 Scott 在本书章节中所描述的那样。较大的一个需要大卫的解决方案。另一件事是
$ git push origin --all
did not push any tags and instead I had to do this:
没有推送任何标签,而是我不得不这样做:
$ git push origin --all
$ git push origin --tags
This may not be obvious from the flag --all and from the book chapter and I realized this after I have deleted the local git svn repo.
这可能从标志 --all 和本书章节中并不明显,我在删除本地 git svn repo 后意识到这一点。