Git与SVN的相互转换

原文连接:https://blog.csdn.net/itluochen/article/details/52411530

一:Git是什么?
  Git是目前世界上最先进的分布式版本控制系统。

二:SVN与Git的最主要的区别?

SVN是集中式版本控制系统,版本库是集中放在中央服务器的,而干活的时候,用的都是自己的电脑,所以首先要从中央服务器哪里得到最新的版本,然后干活,干完后,需要把自己做完的活推送到中央服务器。集中式版本控制系统是必须联网才能工作,如果在局域网还可以,带宽够大,速度够快,如果在互联网下,如果网速慢的话,就纳闷了。

Git是分布式版本控制系统,那么它就没有中央服务器的,每个人的电脑就是一个完整的版本库,这样,工作的时候就不需要联网了,因为版本都是在自己的电脑上。既然每个人的电脑都有一个完整的版本库,那多个人如何协作呢?比如说自己在电脑上改了文件A,其他人也在电脑上改了文件A,这时,你们两之间只需把各自的修改推送给对方,就可以互相看到对方的修改了。

git-svn
git-svn用于Git和SVN的转换,可以把Git仓库迁移成SVN仓库,反之亦可。

详细介绍可见[1],或者命令行输入git-svn。

Bidirectional operation between a Subversion repository and git.

git svn can track a standard Subversion repository, following the common “trunk/branches/tags” layout,

with the --stdlayout option. It can also follow branches and tags in any layout with the -T/ -t/ -b options.

Once tracking a Subversion repository, the git repository can be updated from Subversion by thefetch

command and Subversion updated from git by the dcommit command.

Git迁移到SVN
把Git迁移到SVN?没错,还真有这种需求,虽然较少:)

Git has a feature to work with SVN repositories, git-svn, but that’s intended to check out

existing code from SVN and work on it, not publishing an existing Git tree into a SVN repository.

(1) 依赖包

需要安装subversion-perl、perl-TermReadKey。

Git中带有git-svn,需要确定它的路径可在PATH中找到。

(2) 建立svn项目

在svn服务器中新建svn项目proj。

svnadmin create proj

(3) svn项目的本地初次提交

svn mkdir http://IP:PORT/svn/proj/trunk \

                 http://IP:PORT/svn/proj/branches \

                 http://IP:POPT/svn/proj/tags \

                 http://IP:PORT/svn/proj/docs -m "Initial import"

(4) 提交Git proj项目到SVN proj

进入Git proj项目。

git-svn init -s http://IP:PORT/svn/proj // -s表示svn为标准布局

标准布局的意思就是:

trunk分支为:proj/trunk,可以用-T另外指定

branches分支为:proj/branches,可用-b另外指定

tags分支为:proj/tags,可用-t另外指定

git-svn fetch // 获取SVN proj的更新

r1 = 79563196f21ce4699a04fa4ae24d0ca916bf3acf (refs/remotes/trunk)

trunk分支代表SVN proj的trunk分支

git-svn dcommit // 提交Git proj的更新

注意!这个时候会报错:

Unable to determine upstream SVN information from HEAD history.

Perhaps the repository is empty. at /usr/local/git/libexec/git-core/git-svn line 852.

因为现在Git proj的commits不知道要放到SVN proj的哪个版本之后,即Git proj的这些提交要

放在SVN哪个版本之后(显然是放到SVN的初始提交之后,但是Git proj就是不知道,因为设计者

并不考虑把Git proj转为SVN proj的情况:)

以下是详细描述:

This fails since the git svn command can’t figure out which commits to push: there’s no link

between our original Git repository and the Subversion heads.

To fix this, we can use a Git graft to link them. We’ll tell Git the commit which created the SVN

folder in which we want to store the project is the parent commit of the first commit in our Git

repository.

解决方法

git show-ref trunk // 显示SVN proj trunk分支的HEAD,即r1

79563196f21ce4699a04fa4ae24d0ca916bf3acf refs/remotes/trunk

git log --pretty=oneline master | tail -n 1 // 显示Git proj 的第一个commit

561c439a15f807b8d62551a0c64f939c15489899 initial import

echo “561c439a15f807b8d62551a0c64f939c15489899 79563196f21ce4699a04fa4ae24d0ca916bf3acf” >> .git/info/grafts

把Git proj从561c43开始的提交,添加到SVN proj在795631之后的提交。

git-svn dcommit // 提交Git proj的更新到SVN proj中

这个时候就把Git proj完整的转化成SVN proj,后者完全符合SVN的规范了。

(5) 把git分支提交到svn的分支

如果原来的git仓库不仅有master,还有其它分支,那么怎么把git的分支提交为svn的分支呢?

这个时候就要用到git rebase了。

过程如下:

在远端svn仓库中创建一个新分支。

svn cp URL/trunk URL/branches/remote-branch

更新本端Git仓库。

git-svn fetch

切换到本端的分支。

git checkout local-branch

创建一个用于临时用的分支,其实和local-branch是一样的。

git checkout -b temp-local-branch

关键点来了,把本端分支的所有修改,合并到远端分支。

git rebase remotes/remote-branch

完成上一步以后,远端分支已经含有本端分支的所有commit。

接着可以把修改提交到svn仓库了。

git svn dcommit

最后把本端的临时分支删除。

git branch -D temp-local-branch

此时再查看svn仓库,就会发现branch/remote-branch已经是原来的git分支了。

不过svn分支的日志顺序,相较于原来git分支的可能会有所不同,这是由rebase引起的,正常现象。

(6) 从git分支更新svn分支

修改.git/config,添加远程分支:

[svn-remote “branch”]

url = URL/proj/branches/branch

fetch = :refs/remotes/branch

绑定本地分支和远程分支:

[branch “local-branch”]

remote = .

merge = refs/remotes/branch

修改了本地分支local-branch后,可以直接提交到远端svn的branch分支了。

git-svn dcommit

SVN迁移到Git
相比之前介绍的Git迁移到SVN,SVN迁移到Git才是主流趋势,也更加容易,因为这是git-svn的主要目的。

同样用git-svn来完成迁移:

git-svn primarily can be used to checkout a Subversion repository to a local Git repo and then

push your changes back to the original Subversion repository.

克隆SVN仓库,可以使用-T trunk、-b branches、-t tags来指定对应分支,-s表示采用标准布局。

git-svn clone -s URL/proj proj

相当于执行了git-svn init和git-svn fetch。

把SVN仓库的更新同步到Git。

git-svn rebase

创建.gitignore文件。

git-svn create-ignore

将Git的更新提交到SVN仓库。

git-svn dcommit

Reference
[1]. https://www.kernel.org/pub/software/scm/git/docs/git-svn.html

[2]. http://plpatterns.com/post/234334879/how-to-convert-a-local-branch-in-git-to-a-remote

[3]. http://www.cnblogs.com/kym/archive/2010/08/12/1797937.html

作者:ITluochen
来源:CSDN
原文:https://blog.csdn.net/itluochen/article/details/52411530
版权声明:本文为博主原创文章,转载请附上博文链接!

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
git-svn 是一个桥接工具,用于在 Git 和 Subversion (SVN) 之间进行转换和交互。下面是 git-svn 的安装配置过程。 安装 Git: 1. 在官网 (https://git-scm.com/downloads) 上下载适合你操作系统的 Git 安装程序。 2. 打开安装程序并按照指示进行安装。 3. 验证安装是否成功,在命令提示符或终端上运行 "git --version" 命令,如果出现 Git 的版本号信息,则说明安装成功。 配置 Git: 1. 打开命令提示符或终端,并运行以下命令来配置你的 Git 用户名和邮箱: ``` git config --global user.name "你的用户名" git config --global user.email "你的邮箱地址" ``` 安装 Git-svn 桥接工具: 1. 在命令提示符或终端上运行以下命令安装 Git-svn: - 在 Ubuntu 上使用 apt-get: ``` sudo apt-get install git-svn ``` - 在 macOS 上使用 Homebrew: ``` brew install git-svn ``` - 在 Windows 上使用 Scoop: ``` scoop install git-svn ``` 配置 Git-svn: 1. 在命令提示符或终端上运行以下命令配置 Git-svn: ``` git svn init [SVN 仓库 URL] -s ``` 2. 这将为你的 Git 仓库创建一个指向 SVN 仓库的远程“refs/remotes/origin/trunk”引用。 3. 运行以下命令来下载远程 SVN 仓库的历史记录: ``` git svn fetch ``` 4. 这将下载远程 SVN 仓库的历史记录到你的本地 Git 仓库。 5. 当你想要提交更改时,使用 Git 的命令,例如 "git add"、"git commit"等来管理更改,并使用以下命令将更改推送到 SVN 仓库: ``` git svn dcommit ``` 以上是 git-svn 的安装配置过程。-git提供了更快速、灵活、分布式的版本控制系统,而git-svn桥接工具则使得想要在 git 和 Subversion (SVN)之间进行转换和交互的用户能够灵活使用两种工具。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值