git/svn 使いはじめてみた

以前よりZIGOROuさんからgitネタのブログ書きなよと言われていたので、重い腰を上げて書き始めようと思う。

何はともあれSubversionからリポジトリを取得する。

cd ~/work
git svn clone http://centos5/git-test -T trunk -b branches -t tags

「-T」、「-b」、「-t」オプションは指定しなくてもいいが、指定しておくとgitリポジトリ作成時にリモート追跡ブランチを自動的に設定してくれるので指定しておく。

git branch -r でリモート追跡ブランチの一覧が表示される。

cd ~/work/git-test
git branch -r
  stable-1.0
  tags/release-1.0
  trunk

「-r」オプションなしで現在のブランチ一覧が表示される。「master」はgitがデフォルトで作成するブランチ。

git branch
* master

.git/HEAD の中身を見ることで現在のブランチを確認することができる。

cat .git/HEAD
ref: refs/heads/master

git checkout -b (新しいブランチ名) (読み込むブランチ名) でgitリポジトリからリモート追跡ブランチ stable-1.0 のHEADをチェックアウトして新しいブランチ stable-1.0 を作成してくれる。切り替えも行ってくれる。
「-b」オプションはgitリポジトリに新しいブランチを作るときにのみ使用する。

git checkout -b stable-1.0 stable-1.0
git branch
  master
* stable-1.0

当然 .git/HEAD の中身も切り替わっている。

cat .git/HEAD
ref: refs/heads/stable-1.0

既に存在するブランチの切り替えは「-b」オプションなしで単に git checkout **** でOK。

git checkout master
cat .git/HEAD
ref: refs/heads/master

では実際に編集した内容をgitリポジトリsvnリポジトリにコミットしてみる。と、その前にコミット時に聞かれる名前とメールアドレスを登録しておく。

git config user.name typomaster
git config user.email typomaster@example.com

では、実際に編集して差分を確認しながらgitリポジトリまでコミットしてみる。

vi hoge
aaa
111 <- 追加
vi dir/fuga
bbb
222 <- 追加
git diff
diff --git a/dir/fuga b/dir/fuga
index f761ec1..0f4bdea 100644
--- a/dir/fuga
+++ b/dir/fuga
@@ -1 +1,2 @@
 bbb
+222
diff --git a/hoge b/hoge
index 72943a1..7466234 100644
--- a/hoge
+++ b/hoge
@@ -1 +1,2 @@
 aaa
+111
git status
# On branch master
# Changed but not updated:
#   (use "git add <file>..." to update what will be committed)
#
#       modified:   dir/fuga
#       modified:   hoge
#
no changes added to commit (use "git add" and/or "git commit -a")

期待通り差分が確認できたのでgitリポジトリに add & commit する。

git add .
git diff
(何も表示されない)
git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       modified:   dir/fuga
#       modified:   hoge
git commit
(コミットメッセージを入力する)

以上でgitリポジトリへのコミットは完了。git add . と git commit はまとめて git commit -a で実行できる。
念のためコミットされた差分を確認してみる。

git show HEAD
commit a27965bda41d4951e5f7496f7512c9c9270508d6
Author: typomaster <typomaster@example.com>
Date:   Mon Mar 16 05:02:50 2009 +0900

    git test commit.

diff --git a/dir/fuga b/dir/fuga
index f761ec1..0f4bdea 100644
--- a/dir/fuga
+++ b/dir/fuga
@@ -1 +1,2 @@
 bbb
+222
diff --git a/hoge b/hoge
index 72943a1..7466234 100644
--- a/hoge
+++ b/hoge
@@ -1 +1,2 @@
 aaa
+111

もう1つ前のコミットを確認するには

git show HEAD^
commit ffdaa21c91faf6eb259b0b49b7bba0c140fd29cb
Author: typomaster <typomaster@3076b008-3b7a-4d17-b439-d3a6704986cd>
Date:   Sun Mar 15 08:17:35 2009 +0000

    test commit.


    git-svn-id: http://centos5/git-test/tags/release-1.0@6 3076b008-3b7a-4d17-

で確認できる。2個前は git show HEAD^^, 5個前は git show HEAD~5 で確認できる。

では続けて今コミットしたものをsvnリポジトリにコミットしてみる。とその前にsvnリポジトリで更新があるかもしれないので、それを取得してマージする。

git svn fetch
        M       foo
r7 = c847b24de53d91041bbcb1589b89d65125557404 (trunk)

trunk に変更があったみたい。gitリポジトリのリモート追跡ブランチのtrunkに取り込めた。差分を確認してみる。

git diff trunk
diff --git a/foo b/foo
index f812144..1d60b70 100644
--- a/foo
+++ b/foo
@@ -1,2 +1 @@
 ccc
-ZZZ

確かに差分が確認できた。masterブランチに差分を取り込む。

git rebase trunk
First, rewinding head to replay your work on top of it...

HEAD is now at c847b24... svn commit.

Applying work git test commit.

Wrote tree 39994fc850b22ec09c4f29b25c0dd4c6d59a2eaf
Committed: 29548eca23086c67dcd774921e0308faab51080e

本当に取り込めたか変更したファイルを見てみる。

cat foo
ccc
ZZZ

ちゃんと変更をmasterブランチに取り込めている。あとはgitリポジトリの差分をsvnリポジトリにもコミットする。

git svn dcommit
        M       dir/fuga
        M       hoge
Committed r9
        M       hoge
        M       dir/fuga
r9 = c22116dfe457a8b4ead40690e3c832016ba9deb6 (trunk)
W: HEAD and refs/remotes/trunk differ, using rebase:
:040000 040000 f7de5d9a1d86a2ec2812e3f3bcdce0f17c5aa59f 8b6aa1e01e5f41dbada079881370c1f3d9185c62 M      dirFirst, rewinding head to replay your work on top of it...

HEAD is now at c22116d... work2 git test commit.
Nothing to do.

実際にsvnリポジトリにコミットされたか確認する。またgitリポジトリで差分がないかも確認する。

git diff trunk
(何も表示されない)
git log
commit c22116dfe457a8b4ead40690e3c832016ba9deb6
Author: typomaster <typomaster@3076b008-3b7a-4d17-b439-d3a6704986cd>
Date:   Sun Mar 15 13:03:27 2009 +0000

    work git test commit.


    git-svn-id: http://centos5/git-test/trunk@9 3076b008-3b7a-4d17-b439-d3a670
git svn log
r9 | typomaster | 2009-03-15 22:03:27 +0900 (日, 15  3月 2009) | 2 lines

work git test commit.

svnリポジトリの作業コピーで差分を取得してみる。

svn st -u
       *        3   dir/fuga
       *        2   hoge
状態の背景となるリビジョン:      9
svn up
U    dir/fuga
U    hoge
リビジョン 9 に更新しました。
cat hoge
aaa
111
cat dir/fuga
bbb
222

確かにsvnリポジトリにも変更が反映されている。

git/svn の基本的な使い方の話なのにこんなに長くなってしまった。。。たぶんブログ書くのに慣れてないから。

えっと、本当に基本の基本だけを書いたので次回はコンフリクトが起きたときのマージの仕方を書こうと思う。