How to migrate SVN with history to a new Git repository

這個問題拖了好久…

公司所有新專案的版本管理系統都直接採用 Git 了。唯一還停留在 SVN 的是我手上一個持續了兩年多的專案,因為有其他廠商的工程師參與,所以一直說要轉換 Git,卻沒真的轉換… 一方面是沒有轉換的經驗,一方面是手上工作一多,還能用的 SVN 就繼續吧…. (懶

一直到上週跟客戶開完會後,主管指示快點處理這件事,所以就來試試看怎麼轉換了。

Git 內建 git svn clone 這樣的指令是已知的。不過查資料才知道還要先做 user 名單才能完整轉換。因為 SVN 只有紀錄username,但 Git 是 username + email 的方式,所以要先做一個對應的文字檔。

先說一下,我是在 Mac 下進行的。

Step 1. 以這個指令取得 svn log,並輸出成 SVNuser.txt。

svn log --quiet --xml > SVNuser.txt

這份文字檔打開是長這樣…

<?xml version="1.0" encoding="UTF-8"?>
<log>
<logentry
revision="1251">
<author>user_a</author>
<date>2013-09-26T10:00:20.567200Z</date>
</logentry>
<logentry
revision="1250">
<author>user_a</author>
<date>2013-09-25T07:01:05.143127Z</date>
</logentry>
<logentry
revision="1249">
<author>user_a</author>
<date>2013-09-24T06:43:11.776178Z</date>
</logentry>

我用 Sublime TextSort LinesPermute Lines > Unique 兩個功能快速濾掉不重要的資訊。剩下…

<author>user_a</author>
<author>user_b</author>
<author>user_c</author>

手動改成這樣… svn_username = git_username <user_email> 的型式

user_a = user_a <user_a@example.com>
user_b = user_b <user_b@example.com>
user_c = user_b <user_c@example.com>

恭禧完成第一個步驟。

Step 2. 用 git svn 將 SVN 紀錄讀回來

git svn clone --prefix=origin/ --no-metadata -A SVNuser.txt https://帳號:密碼@svn/repos/here/trunk

然後等它跑完就好了。

Step 3. 記得把 Git push 到新的 remote repo。

轉換後還是可以用 Git 去操作 SVN,不過我沒這麼做過,反正都轉換完了。


Posted

in

, ,

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *