svn迁移到git下全过程

来源:互联网 发布:json实体类工具 编辑:程序博客网 时间:2024/04/29 04:08

迁移记录,根据实际操作,有改动和改进地方。

==========================================================================

(备注:收费的)

SubGit 2.0版本,开始支持远程subversion版本库,因此创建Git镜像,无需 shell 访问subversion版本库了。

项目地址:

http://subgit.com/index.html

==========================================================================

如何将远程SVN版本库搬到本地 

From:http://nowing.iteye.com/blog/844608

经常在宽带网络状况不佳的时候,访问互联网上的SVN库是一件极其痛苦的事情,更别说要查看版本库的日志信息了。此时如果可以将远程版本库整个同步到本地,然后所有操作都在本地的版本库上进行,好处就不用多说了。幸运的是SVN已经提供了相应的功能,具体操作步骤如下: 

1)在本地建立一个新的版本库: 

svnadmin create D:\test 

2)创建钩子文件pre-revprop-change.bat:(windows环境里是D:\test\hooks\pre-revprop-change.bat) 

文件中只需要一行内容即可“exit 0”。 

3)初始化同步操作: 

svnsync init file:///D:/test <远程SVN库的URL> 

(如果需要用户名/密码,则按提示输入。成功后命令行将输出信息:复制版本 0 的属性) 

4)执行同步操作: 

svnsync sync file:///D:/test 

(如果需要用户名/密码,则按提示输入。如果远程SVN库数据较多,需要慢慢等待) 

5)如果远程SVN库有了新的更新,只需重复执行步骤4即可。 

-------------- 

如何将SVN仓库转换为Git仓库 

按如下步骤操作就可以将SVN仓库完整的转换为Git仓库: 

1) 将远程SVN仓库搬到本地(这一步主要是为了提高转换的速度,也可以忽略) 

这里假设最终要转换的SVN仓库为file:///tmp/test-svn 

2) 使用git svn clone命令开始转换 

$ git svn clone file:///tmp/test-svn -T trunk -b branches -t tags 

git svn clone 命令会把整个Subversion仓库导入到一个本地的Git仓库中。这相当于针对所提供的 URL 运行了两条命令git svn init加上gitsvn fetch。因Git需要提取每一个版本,每次一个,再逐个提交。对于一个包含成百上千次提交的项目,花掉的时间则可能是几小时甚至数天(如果你的SVN仓库是远程网络访问的,先执行上面第一步的操作还是有点好处的。不过项目通常提交次数都不少,漫长的等待是少不了的啦,慢慢等吧)。 

-T trunk -b branches -t tags告诉Git该Subversion仓库遵循了基本的分支和标签命名法则。如果你的主干(trunk,相当于Git里的master分支,代表开发的主线)、分支或者标签以不同的方式命名,则应做出相应改变。由于该法则的常见性,可以使用-s来代替整条命令,它意味着标准布局(s是Standard layout的首字母),也就是前面选项的内容。下面的命令有相同的效果: 

$ git svn clone file:///tmp/test-svn -s 

注意本例中通过 git svn 导入的远程引用,Subversion的标签是当作远程分支添加的,而不是真正的Git标签。导入的Subversion仓库仿佛是有一个带有不同分支的tags远程服务器。用“$ git show-ref”就可以看到转换后Git仓库的相关情况,结果类似如下: 

$ git show-ref 

1cbd4904d9982f386d87f88fce1c24ad7c0f0471 refs/heads/master 

aee1ecc26318164f355a883f5d99cff0c852d3c4 refs/remotes/my-calc-branch 

03d09b0e2aad427e34a6d50ff147128e76c0e0f5 refs/remotes/tags/2.0.2 

50d02cc0adc9da4319eeba0900430ba219b9c376 refs/remotes/tags/release-2.0.1 

4caaa711a50c77879a91b8b90380060f672745cb refs/remotes/tags/release-2.0.2 

1c4cb508144c513ff1214c3488abe66dcb92916f refs/remotes/tags/release-2.0.2rc1 

1cbd4904d9982f386d87f88fce1c24ad7c0f0471 refs/remotes/trunk 

而普通的 Git 仓库是类似如下模样: 

$ git show-ref 

83e38c7a0af325a9722f2fdc56b10188806d83a1 refs/heads/master 

3e15e38c198baac84223acfc6224bb8b99ff2281 refs/remotes/gitserver/master 

0a30dd3b0c795b80212ae723640d4e5d48cabdff refs/remotes/origin/master 

25812380387fdd55f916652be4881c6f11600d6f refs/remotes/origin/testing 

这里有两个远程服务器:一个名为gitserver,具有一个master分支;另一个叫origin,具有master和testing两个分支。 

3) 获取SVN服务器的最新更新到转换后的Git仓库(这步通常在连续的转换过程中就没必要了) 

$ git svn rebase 

4) 转换SVN仓库的svn:ignore属性到Git仓库的.gitignore文件 

$ git svn create-ignore 

该命令自动建立对应的.gitignore文件,以便下次提交的时候可以包含它。如果在生成.gitignore文件前想先查看一下,运行命令“git svn show-ignore”即可。 

5) 转换SVN的标签为Git标签 

$ cp -Rf .git/refs/remotes/tags/* .git/refs/tags/ 

$ rm -Rf .git/refs/remotes/tags 

该命令将原本以 tag/ 开头的远程分支的索引变成真正的(轻巧的)标签。 

这个在Window下试过不行,报”cp: cannot stat `.git/refs/remotes/tags/*': No such file or directory“的错误,可以使用如下两个标准命令处理: 

$ git tag tagname tags/tagname     ----用指定的分支创建一个Git标签 

$ git branch -r -d tags/tagname    ----删除指定的远程分支 

6) 转换SVN的分支为Git分支 

$ cp -Rf .git/refs/remotes/* .git/refs/heads/ 

$ rm -Rf .git/refs/remotes 

该命令把refs/remotes下面剩下的索引变成Git本地分支 

7) 最后把转换后的本地Git仓库推到公共的Git服务器 

$ git remote add origin [远程Git服务器地址] 

$ git push origin master --tags 

所有的标签和主干现在都应该整齐干净的躺在新的Git服务器里了。如果要将分支也同步到远程Git服务器,将--tags改为--all。

==========================================================================

+

+

+

=




git的出现,让svn深受打击,大家纷纷转战git。没错,我也移情别恋了,一下就描述一下抛弃svn,迷上git的过程吧

简单粗暴,命令如下:

git svn clone https://localhost:8443/svn/www/ –no-metadata –trunk=trunk www

参数说明:

no metadata 参数是阻止git 导出svn包含的附加信息,这样提交到Git的记录就会显得很“干净”

trunk 主分支

www 创建的git项目名称

执行过程可能会有svn帐户的输入,反正就用户名跟密码吧

2.代码克隆下来之后,这已经是一个git下checkout的项目了,只是他还没有代码库源,简单的说就是他还没有个git下固定的家(他生母是svn,得给他找个继母),因此要先在你的github,或者你们公司内部搭建的git平台,创建一个 属于www项目的git库,当然也可以自己本地创建,这里就不详细说了,等下说到代码部署会说到。

比如代码库如下:

git@gitlab.xxx.com:second/test.git

为了证明www确实还没有新妈,你可以用命令:

git remote -v 查看一下是否有源,执行完很明显,什么都没显示

接下来就是把你的www库领回test.git这个妈的家里,命令如下:

git remote add git@gitlab.xxx.com:second/test.git

执行完之后,你可以再执行一次git remote -v 你会发现如下:

 

origin刚才谷歌翻译了一下,是起源的意思,其实在执行git remote add 原本是需要给源取个名字的,比如发布源 release

就需要这样输入git remote add  release git@gitlab.xxx.com:second/test.git,因为www还没有源,所以添加的就变成默认的源,也就是起源了,因此省略了 origin

到这里,已经完成了认识新妈的过程,接下来干嘛,没错,所有代码领回家,命令如下

git push

代码从svn 到git 就算完成了。

其实在这个过程里似乎漏了一步,没有在git下创建分支,git branch -a  查看一下,发现已经自动创建了默认主分支master

案例:

1.物理环境
Gitserver    Centos5.8    192.168.1.245
Svnserver    Centos5.8    192.168.1.108  

2.建立SVN用户到git用户的映射文件,文件格式如下:

cat /tmp/userinfo.txtdavid=sfzhang<shifeng_zhang88@163.com>yanni=yanni<yanni_liu88@163.com>

3.通过git svn clone克隆一个git版本库,SVN里面包含trunk,branchestags

git svn clone svn://192.168.1.108:9999/yanzi/ --no-metadata --authors-file=userinfo.txt --trunk=trunkmobile --tags=tags --branches=branches --ignore-refs=refs/remotes/yanzi-.*  yanzi
  • 参数–no-metadata表示阻止git导出SVN包含的一些无用信息
  • 参数–authors-file表示SVN账号映射到git账号文件,所有svn作者都要做映射
  • 参数–trunkmobile表示主开发项目
  • 参数–branches表示分支项目,--ignore-refs表示不包含后面的分支项目
  • 参数yanzi表示git项目名称

4.通过git log 查看项目提交的历史记录,包括作者,日照,和提交注释信息等。

cd yanzigit logcommit 3c4907782804096ea3fa3fb5419dcce610e56f1fAuthor: david <shifeng_zhang88@163.com>Date:   Fri May 10 10:27:50 2013 +0000

5.git版本库里面tag都是branches(分支),首先列出当前所有的分支。

cd yanzigit branch -r  tags/mobile_1.0.0  tags/mobile_1.0.1  trunk  yanziios1.0.1-build-2223-branch-002

6.手动将branches分支转换为tags

git tag mobile_1.0.0 tags/mobile_1.0.0git tag mobile_1.0.1 tags/mobile_1.0.1

7.将多余的branches删除掉

git branch -r -d tags/mobile_1.0.0Deleted remote branch tags/mobile_1.0.0 (was d50002b).git branch -r -d tags/mobile_1.0.1Deleted remote branch tags/mobile_1.0.1 (was e7b78a2).

8.再次列出当前的所有分支。

git branch -r  trunk  yanziios1.0.1-build-2223-branch-002

9.建立git仓库并初始化版本库。

mkdir -p /data/gitdata/yanziios.gitcd /data/gitdata/yanziios.git/git init --bareInitialized empty Git repository in /data/gitdata/yanziios.git/

10.yanziios.git的属主修改为git用户。

chown git yanziios.git -Rls -l yanziios.git/total 64drwxr-xr-x 2 git root 4096 May 22 12:25 branches-rw-r--r-- 1 git root   66 May 22 12:25 config-rw-r--r-- 1 git root   73 May 22 12:25 description-rw-r--r-- 1 git root   23 May 22 12:25 HEADdrwxr-xr-x 2 git root 4096 May 22 12:25 hooksdrwxr-xr-x 2 git root 4096 May 22 12:25 infodrwxr-xr-x 4 git root 4096 May 22 12:25 objectsdrwxr-xr-x 4 git root 4096 May 22 12:25 refs

11.添加远程git服务器地址

git remote add origin git@192.168.1.245:/data/gitdata/yanziios.git

 

12.用git push命令推送全部的分支和标签信息到git服务器上面。

git push origin master --tags

13.SVN迁移到Git测试,在客户端用SourceTree工具克隆一个Git服务端仓库yanziios.git

14.SourceTree图形界面里面可以看到git用户提交的Graph信息,描述信息(Description),日期,作者和版本号等信息。

总结:

  • 在运行git svn clone svn: 命令时出现下面的错误:Can‘t locate SVN/Core.pm in @INC (@INC contains: /usr/local/git/lib/perl5/site_perl/5.8.需要安装subversion-perl软件包。
  • 在运行git pull git@192.168.1.245:/data/gitdata/yanziios.git时出现下面错误:Can’t locate Term/ReadKey.pm in @INC (@INC contains:需要运行下面命令:

Pull up the CPAN teminal:
perl -MCPAN -e shell
Once at the cpan prompt install the needed module:
cpan> install Term::ReadKey

  • 需要在本机用sshkeygen t rsa C your_email_name生成KEY认证文件,然后把公钥id_rsa.pub追加到git服务器的git用户家目录authorized_keys文件里面。
  • SVN 只有trunk,branches,没有tags导出方法。
git svn clone svn://192.168.1.10:8888/svnproject/ --no-metadata --authors-file=userinfo.txt --trunk=trunk  --branches=branches --ignore-refs=refs/remotes/yanziios1.* gitproject
  • git clone 远程分支git clone git@192.168.1.222:/data/gitdata/yanzi/test.gitbranch  testbuild1442branch001 test001
  • 更多的错误详见making git-svn work on mac tiger

转载请注明:爱开源 » svn 迁移到git下全过程


Stackoverflow Q & A:

I'm trying to migrate a project from SVN to git. This is the command I use:

$ git svn clone http://oursvnserver/ --no-metadata -A ../authors-transform.txt --trunk=path/to/trunk --branches=path/to/branches --tags=path/to/tags . --username=mysvnusername --prefix=origin/

The current directory is the directory that I want to become a repository. authors-transform.txtis most definitely in the right location. The project uses the standard layout, but it does not exist at the root of the repository. (Unfortunately, someone long ago started the practice of just stuffing all projects into the same repository. That's why I specify --trunk--branches, and --tags.) It seems to check out fine. Then I try to generate an ignore file and get this incredibly cryptic error:

$ git svn show-ignoreconfig --get svn-remote.svn.fetch :refs/remotes/git-svn$: command returned error: 1

It appears to be running some other command in the process, so I made some guesses:

$ git config --get svn-remote.svn.fetch :refs/remotes/git-svn || echo $?1$ git config --get svn-remote.svn.fetch :refs/remotes/git-svn$ || echo $?1

So maybe I'm guessing right about what command it's calling? But that doesn't really help.

What does this error actually mean? What can I try to resolve it?

Using msysgit 1.9.4.

Answers:

So this is caused by git not being able to find a matching fetch entry in the svn-remote section of the $GIT_DIR/config file. Without an argument to git svn show-ignore, it doesn't know what to look for in that section. (Maybe my tracking isn't set up right?)

To deal with this, you need to first specify which remote you want it to look at:

git svn show-ignore --id=origin/trunk

(Note that I specified the "origin" prefix when performing the clone. Vary your command accordingly.)

Secondly, this still failed with a very similar error for my branches. It worked fine for trunk, but not branches. This is because there was no fetch entry for the branches. To add them, use this command (with adjustments for prefix):

git config --add svn-remote.svn.fetch path/to/branches/branchname:refs/remotes/origin/branchname

To see all the fetch entries and make sure it worked:

git config --get-all svn-remote.svn.fetch

I'm not sure why only an entry for trunk is set in the beginning; maybe I did something wrong with the clone.


0 0
原创粉丝点击