用 Git 來管理 SVN 專案

今天忽然想來修一下 WP Plugin 的 bug,但太久沒用,一時忘記怎麼把 git 和 svn 整合起來,花了好一段時間,還是把它寫下來以備不時之需。

如果之前已經整合過的,大概不用經過這樣的步驟,應該只要 git svn rebase, git svn dcommit 應該就可以了。以下的步驟只適用於第一次用 git 來管理 svn 專案的情況。

目前手邊還接觸得到的 svn 只有 wordpress plugin,以下例子以 Featured Image via URL 這個 plugin 來說明:

找到最開始的 rev

$ svn log -r 1:HEAD --limit 1 http://plugins.svn.wordpress.org/featured-image-via-url
------------------------------------------------------------------------
r730489 | plugin-master | 2013-06-24 02:48:28 +0800 (一, 24  6 2013) | 1 line

adding featured-image-via-url by tsaiid
------------------------------------------------------------------------

以最初的 rev 將 repo clone 回來

$ git svn clone -s -r570197 http://plugins.svn.wordpress.org/featured-image-via-url
Initialized empty Git repository in /Users/tsaiid/git/featured-image-via-url/.git/
Using higher level of URL: http://plugins.svn.wordpress.org/featured-image-via-url => http://plugins.svn.wordpress.org
r730489 = f4309b8050ee3afa04983fb3693dabbd579d8b65 (refs/remotes/trunk)
Checked out HEAD:
  http://plugins.svn.wordpress.org/featured-image-via-url/trunk r730489

進行 Fetch 資料

看 commit 量的多寡而定,可能會需要數分鐘至數小時

$ git svn fetch
    A   readme.txt
    A   featured-image-via-url.php
r730608 = 373506aa402fba7cf7b3e5e15ef0584d06a066cc (refs/remotes/trunk)
Found possible branch point: http://plugins.svn.wordpress.org/featured-image-via-url/trunk => http://plugins.svn.wordpress.org/featured-image-via-url/tags/0.1, 730608
Found branch parent: (refs/remotes/tags/0.1) 373506aa402fba7cf7b3e5e15ef0584d06a066cc
Following parent with do_switch
Successfully followed parent
r730609 = c8188362a964505b058d646eb63fe7a71497d953 (refs/remotes/tags/0.1)
    M   readme.txt
r730737 = 2972f30b8b4aed640b30b9278d45f84c6755b6a2 (refs/remotes/trunk)
    M   readme.txt
r738700 = 7dff7ca4c121b7acf3d5467da316fda621ab81a8 (refs/remotes/trunk)
    M   readme.txt
    A   README.md
r826881 = 3ce6ebedb3234e3a88a454c66148b65cc35cd6fe (refs/remotes/trunk)
    M   featured-image-via-url.php
r826884 = 8b9d2a28f7b2861c0e831a85b9866d877d8344e8 (refs/remotes/trunk)

加入 Git Remote Origin

底下以 GitHub 的 repository 為例

$ git remote add origin git@github.com:tsaiid/wp-plugin-featured-image-via-url.git

svn rebase

第一次整合時,不可以先將 origin pull 回來,之後就要碰上一堆的 conflict,先進行 svn rebase

$ git svn rebase
First, rewinding head to replay your work on top of it...
Fast-forwarded master to refs/remotes/trunk.

pull origin

$ git pull --rebase origin master
remote: Counting objects: 34, done.
remote: Compressing objects: 100% (22/22), done.
remote: Total 27 (delta 8), reused 22 (delta 3)
Unpacking objects: 100% (27/27), done.
From github.com:tsaiid/wp-plugin-featured-image-via-url
 * branch            master     -> FETCH_HEAD
First, rewinding head to replay your work on top of it...
Fast-forwarded master to 8a3131720dcfdd1b79d62e4c1222793f8b9e0c8a.

接下來就如同平時在操作 git 一樣,如果需要 update svn,可執行:

$ git svn dcommit