美文网首页
SVN到Git的一键迁移脚本(保留所有分支、Tag及提交记录)

SVN到Git的一键迁移脚本(保留所有分支、Tag及提交记录)

作者: 简单点的笨演员 | 来源:发表于2021-05-14 19:49 被阅读0次

注意:
1、由于用到grep、sort、awk,因此在Windows环境中需要在bash中运行,如果没有的话先安装Git即可;
2、需要安装svn的命令号工具,如果没有的话需要安装下:


一、准备SVN用户到Git用户的映射文件

在SVN中,每一个人提交时都需要在系统中有一个用户,它会被记录在提交信息内。如果想要将SVN用户映射到一个更好的Git作者数据中,你需要一个SVN用户到Git用户的映射。参考《一键获取所有SVN提交作者用户名的列表》获取所有SVN提交作者,参考下面的格式建立authors.txt文件:

svn_username1=git_username1<sfddsfsdf@xxx.com>
svn_username2=git_username2<sfddsfsdf@xxx.com>
svn_username3=git_username3<sfddsfsdf@xxx.com>
.....

二、保存下面脚本为:svn2git.sh

保存到与authors.txt同一目录,并且修改里面的SVN地址。

# 要转换的SVN地址
SVN_URL=http://127.0.0.1:8080/svn/test/

# svn转git
git svn clone $SVN_URL "./svn2git" --no-metadata --trunk=trunk --branches=branches --tags=tags  --authors-file ./authors.txt --no-minimize-url eisp-eipsc-parent-to-git

cd svn2git

# 创建本地标签并删除对应的远程分支
git for-each-ref refs/remotes/origin/tags |cut -d / -f 5-|grep -v @| while read tagname; do git tag "$tagname" "origin/tags/$tagname"; done
git for-each-ref refs/remotes/origin/tags |cut -d / -f 5-|grep -v @| while read tagname; do git branch -r -d "origin/tags/$tagname"; done

# 在本地针对每一个远程分支创建对应的本地跟踪分支
git for-each-ref refs/remotes/origin/ |cut -d / -f 4-|grep -v @| while read branchname; do git branch "$branchname" "refs/remotes/origin/$branchname"; done
git for-each-ref refs/remotes/origin/ |cut -d / -f 4-|grep -v @| while read branchname; do git branch -r -D "origin/$branchname"; done

# 删除无用的trunk分支
git branch -d trunk

# 删除与SVN的关联
rm -rf .git/svn

# 删除所有的远程分支
for i in $(git branch -r); do git branch -D -r "$i"; done;

# 删除SVN中被删除的分支
svn ls $SVN_URL/branches | sed 's|/$||' > svn-branch.txt
git branch |sed 's|^\*||' |sed 's|^[[:space:]]*||' | grep -v '^origin/tags/' | sed 's|origin/||' > git-branch.txt
diff -u git-branch.txt svn-branch.txt |grep -v '^--'|grep '^-' |sed 's|-||'|grep -v '^master' > deleted-branch.txt
for i in $(cat deleted-branch.txt); do echo "$i"; git branch -D "$i"; done;

# 删除SVN中被删除的tags
svn ls $SVN_URL/tags | sed 's|/$||' > svn-branch.txt
git tag  > git-branch.txt
diff -u git-branch.txt svn-branch.txt |grep -v '^--'|grep '^-' |sed 's|-||'|grep -v '^master' > deleted-branch.txt
for i in $(cat deleted-branch.txt); do echo "$i"; git tag -d "$i"; done;

三、执行脚本开始转换

在终端下(windows在bash下),切换到 svn2git.sh 所在的目录,执行:sh ./svn2git.sh执行需要比较久的时间,需要耐心等待 ^o^。转换完成后,转换结果在当前目录下的svn2git。

参考:
http://www.meilongkui.com/archives/1965

相关文章

  • SVN到Git的一键迁移脚本(保留所有分支、Tag及提交记录)

    注意:1、由于用到grep、sort、awk,因此在Windows环境中需要在bash中运行,如果没有的话先安装G...

  • SVN迁移Git

    SVN迁移Git ①本地SVN库转本地Git库 情况一:不想保留svn提交记录 删除svn目录文件中的.svn文件...

  • Git仓库迁移

    迁移Git仓库 使用场景:需要迁移某个git仓库到另一个不同的仓库 需要保留所有分支和历史提交 操作步骤 clon...

  • git迁移(全量分支、tag 和 仅主干两种)

    全量分支tag一起迁移,包括提交记录 1.先克隆老项目的镜像git clone --mirror old.git ...

  • git常见使用 场景

    给分支 标记tag git checkout branchName切换到 分支 git log查看分支 提交记录i...

  • Git项目迁移

    为了保留历史提交记录或远程所有分支信息等,最好使用 git clone —bare old @git -> ...

  • svn迁移到git那些事

    最近项目要求需要把项目从svn迁移到git,并保留原来的一些提交修改记录。在此做个记录,说一下迁移步骤:网上有很多...

  • git merge git rebase

    git merge 在合并的分支上会有一个新的提交,并且新提交有两个parent,会保留合并分支的所有提交记录。 ...

  • SVN项目迁移至Git,保留SVN提交记录

    1. 先在Git服务端创建一个空白版本库,得到Git地址。 2. 新建SVN中的账号和Git中的账号对应关系use...

  • git仓库完整迁移

    git仓库迁移包括tag、分支等所有迁移 步骤: 1.打开终端cd到需要转移的代码文件夹中 2.使用终端查看所有的...

网友评论

      本文标题:SVN到Git的一键迁移脚本(保留所有分支、Tag及提交记录)

      本文链接:https://www.haomeiwen.com/subject/hrfcjltx.html