与《SVN迁移至GitLab,并附带历史提交记录》用的 git svn clone
不同,本文使用svn2git来迁移项目代码。
一、准备工作
- 安装Git环境,配置本地git账户信息:
git config --global user.name "XXX"
git config --global user.email "XXX@mail.com"
可通过命令git config -l 查看结果
- Create a new repository
git clone http://xxx/testproject.git
cd testproject
touch README.md
git add README.md
git commit -m "add README"
git push -u origin master
- Push an existing folder
cd existing_folder
git init
git remote add origin http://xxx/testproject.git
git add .
git commit -m "Initial commit"
git push -u origin master
- Push an existing Git repository
cd existing_repo
git remote rename origin old-origin
git remote add origin http://xxx/testproject.git
git push -u origin --all
git push -u origin --tags
- 安装
git-svn
,运行git svn -h
,若正常输出帮助信息,说明安装没问题。 - 安装
ruby
,运行ruby -h
,若正常输出帮助信息,说明安装没问题。 - 执行下面命令安装svn2git,注意下面命令依赖ruby。
gem install svn2git
再运行svn2git -h
,若正常输出帮助信息,说明安装没问题。
- svn账号与git账号映射,新建文件 userinfo.txt,左侧是SVN的账号,右侧是git账号信息,用=号连接:
svn1=git1<git1@mail.com>
svn2=git2<git2@mail.com>
svn3=gitx<gitx@mail.com>
注意:
- 需要将涉及到的同步记录中的用户都做映射,否则在同步时可能会报错
- 对于无法对应的人员,如离职人员,有SVN账号,但是没有现在的GIT账号,保险起见,可以采用无关紧要的账号进行对应,或者单独建一个账号也可以,例如使用的test账号进行标识的
二、导出SVN项目至本地
- 创建空文件夹用于存放SVN的代码,并将userinfo.txt文件放置在同一层级
│ userinfo.txt
└─ MyProject
- 鼠标右键 选中Git Bash Here 进入git命令行,执行:
svn2git svnUrl --authors userinfo.txt -v
这里的
-v
会输出执行过程的信息,方便debug。此处的 -r 12345:HEAD 表示从svn的12345版本到最新版本的提交记录,避免拉取所有数据量过大,操作耗时高。svnurl是svn项目地址。如果需要拉取所有记录,可去除 -r 12345:HEAD
- 其它用法:
1、标准的项目 (trunk, branches, tags)
svn2git http://svn.example.com/path/to/repo --authors userinfo.txt -v
2.非标准的项目 (trunk, tags)
svn2git http://svn.example.com/path/to/repo --trunk dev --tags rel --nobranches --authors userinfo.txt -v
3.非标准的项目 (trunk)
svn2git http://svn.example.com/path/to/repo --trunk trunk --nobranches --notags --authors userinfo.txt -v
4.非标准的项目 (根目录就是trunk)
svn2git http://svn.example.com/path/to/repo --rootistrunk --authors userinfo.txt -v
三、与远程git仓库关联起来
- 进入项目文件夹,
cd MyProject
- 关联远程事先创建好的git项目
git remote add origin gitURL
如:git remote add origin http://192.168.1.xx:5678/MyProject.git
- 查看关联情况:
git remote -v
- 如果关联有误,可以删除关联
git remote rm origin
- 推送本地项目到git仓库
- 提交记录之前,先拉取远程git项目文件(如:README.md),否则直接提交会报错
git pull --rebase origin master
- 拉取成功后,推送主分支
trunk
代码
git push -u origin --all
或
git push --all origin
- 推送
tags
代码
git push -u origin --tags
ref:
1.从 SVN 到极狐GitLab的迁移指南
2. https://github.com/nirvdrum/svn2git
四、错误问题
- 在windows有下面错误:
git checkout -f master
D:\MyProject>svn2git https:/xxx.com:8443/svn/SVN2GIT/ --authors authors.txt -v
Running command: git svn init --prefix=svn/ --no-metadata --trunk='trunk' --tags='tags' --branches='branches' https:/xxx.com:8443/svn/SVN2GIT/
Initialized empty Git repository in D:/MyProject/.git/
Running command: git config --local --get user.name
Running command: git config --local svn.authorsfile authors.txt
Running command: git svn fetch
Running command: git branch -l --no-color
Running command: git branch -r --no-color
Running command: git config --local --get user.name
Running command: git config --local --get user.email
Running command: git checkout -f master
error: pathspec 'master' did not match any file(s) known to git
command failed:
git checkout -f master
去安装目录C:\Ruby30-x64\lib\ruby\gems\3.0.0\gems\svn2git-2.4.0\lib\svn2git
,找到migration.rb
文件,把#{trunk}
、 #{tag}
、#{branch}
两边的单引号删除,再重新执行上面命令。
参考自:svn2git - command failed
Cannot setup tracking information
Running command: git branch --track "Test" "remotes/svn/xxx"
fatal: Cannot setup tracking information; starting point 'remotes/svn/xxx' is not a branch.
同理,找到migration.rb
文件,搜索--track
,如下:
修改前:
status = run_command("git branch --track \"#{branch}\" \"remotes/svn/#{branch}\"", false)
修改后:
status = run_command("git branch \"#{branch}\" \"remotes/svn/#{branch}\"", false) # remove '--track'
参考:svn2git: Cannot setup tracking information
Software caused connection abort
错误
Error : Software caused connection abort: Error running context: Software caused connection abort at C:/Program Files/Git/mingw64/share/perl5/Git/SVN/Ra.pm line 312
网上说是SVN服务器连接超时,但是修改了增加了超时时间也无效。后来推测是mingw64
命令工具问题,换到ubuntu环境(用的是wsl)确实没有出现问题。但是又引入了下面问题。。。
- svn2git运行时需要输入账号密码,因为无法像windows那样弹出输入窗,所以无法执行下去,出现下面错误:
#<Thread:0x0000562e9d12e00a /var/lib//gems/3.0.0/gems/svn2git3-3.0.3/lib/svn2git/migration.rb:431 run> terminated with exception (report_on_exception is true):
Traceback (most recent call last):
2: from /var/lib//gems/3.0.0/gems/svn2git3-3.0.3/lib/svn2git/migration.rb:432:in `block (2 levels) in run_command'
1: from /var/lib//gems/3.0.0/gems/svn2git3-3.0.3/lib/svn2git/migration.rb:432:in `loop'
/var/lib//gems/3.0.0/gems/svn2git3-3.0.3/lib/svn2git/migration.rb:438:in `block (3 levels) in run_command': undefined local variable or method `stdin' for #<Svn2Git::Migration:0x0000562f9d126af1> (NameError)
Did you mean? String
解决方法是使用默认的:
1、rm -rf ~/.subversion/auth/svn.ssl.server/ #先删除svn认证信息
2、svn list svnUrl #执行后,会提示输入r, t, p 三个选项,这时要选择p
3、svn2git http://xxx/path/to/repo --authors userinfo.txt -v #再执行svn2git命令,不再要求输入svn账号信息