最新消息:

svn 迁移到git下全过程

git admin 37522浏览 0评论

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.txt
david=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 yanzi
git log
commit 3c4907782804096ea3fa3fb5419dcce610e56f1f
Author: david <shifeng_zhang88@163.com>
Date:   Fri May 10 10:27:50 2013 +0000

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

cd yanzi
git 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.0
git tag mobile_1.0.1 tags/mobile_1.0.1

7.将多余的branches删除掉

git branch -r -d tags/mobile_1.0.0
Deleted remote branch tags/mobile_1.0.0 (was d50002b).
git branch -r -d tags/mobile_1.0.1
Deleted 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.git
cd /data/gitdata/yanziios.git/
git init --bare
Initialized empty Git repository in /data/gitdata/yanziios.git/

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

chown git yanziios.git -R
ls -l yanziios.git/
total 64
drwxr-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 HEAD
drwxr-xr-x 2 git root 4096 May 22 12:25 hooks
drwxr-xr-x 2 git root 4096 May 22 12:25 info
drwxr-xr-x 4 git root 4096 May 22 12:25 objects
drwxr-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
  • 更多的错误详见

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

您必须 登录 才能发表评论!