如何在从SVN迁移到Git后列出和获取远程分支?
如何在从SVN迁移到Git后列出和获取远程分支?
我将我们的SVN仓库迁移到了Git并推送到了一个中央仓库。我们有相当多的标签和分支,但是不知何故我们无法在Git客户端中列出和获取它们。这很奇怪,因为标签和分支在服务器上似乎是可用的。
在Jon Maddox的博客文章、Marc Liyanage的博客文章和Casey在Stack Overflow上的回答的帮助下,我最终找到了一个解决方案。感谢大家的帮助!
以下是我的操作步骤。首先,我创建了一个包含SVN提交者的文件:
local$ svn log svn://server/opt/svn/our_app |grep ^r[0-9] | cut -f2 -d\| |sort |uniq | tee ~/users.txt alice bob eve local$ vim ~/users.txt local$ cat ~/users.txt alice = Alice Malicebob = Bob Hope eve = Eve Leave
然后,我从我们的SVN仓库创建了一个Git仓库:
local$ mkdir our_app local$ cd our_app local$ git svn init --stdlayout svn://server/opt/svn/our_app local$ git config svn.authorsfile ~/users.txt local$ git svn fetch local$ git svn create-ignore local$ git commit -m 'added .gitignore, created from svn:ignore' local$ for remote in `git branch -r`; do git checkout -b $remote $remote; done
最后一步非常重要,因为否则在从远程仓库克隆时无法获取分支/标签。无论如何,我将这个推送到了一个新的远程仓库:
local$ ssh server server$ mkdir /opt/git/our_app.git server$ cd /opt/git/our_app.git server$ git --bare init server$ git config core.sharedrepository 1 server$ git config receive.denyNonFastforwards true server$ find objects -type d -exec chmod 02770 {} \; server$ exit local$ git remote add origin ssh://server/opt/git/our_app.git local$ git push --mirror
对远程仓库进行全新克隆后,一切都可用:
local$ git clone ssh://server/opt/git/our_app.git local$ cd our_app local$ git branch -a * master remotes/origin/master remotes/origin/pre-svn-move remotes/origin/tags/mytag-0.1 remotes/origin/tags/mytag-0.2 remotes/origin/trunk remotes/origin/mybranch-1 remotes/origin/mybranch-2
现在可以检出一个远程分支:
local$ git checkout -t origin/mybranch-1 local$ git branch master * mybranch-1
再次强调一下:这个指南包括有关远程标签和分支的可用性的提示,将内容镜像到远程仓库以及在svn:ignore中重用值。我之前没有在一个指南中找到所有这些内容。
最后一点说明:ebneter关于svn2git的提示也非常棒,因为这个工具可以将标签保留为标签,而git-svn会将它们转换为分支。另一方面,我无法让"git svn create-ignore"与svn2git一起工作...
问题的原因是在SVN到Git迁移后,无法列出和获取远程分支。解决方法是删除git config branch.master.remote origin
命令,并在推送到裸仓库之前跟踪所有远程分支。
当然,问题的解决过程中还出现了其他一些情况。在初始推送之前,使用git branch --track
命令为所有分支跟踪远程分支。然后执行git push --mirror
命令进行推送。推送后,服务器上的所有标签和分支都会出现两次,但在客户端上执行git clone
命令后,git branch -r
命令列出的标签和分支是正确的,不会重复。最后,如果执行git checkout -b mybranch-1 origin/mybranch-1
命令,可以获取到最新的主干代码,没有任何不同的标签和分支。
这个问题可能是由于迁移过程中的一些配置问题导致的,但通过上述解决方法,可以成功列出和获取远程分支。