git logでマージコミットの中の変更を表示する

2013/11/11 | 所要時間 約2分

git

git logを実行するとコミット履歴が見れて、オプションによっては各コミット毎にどのファイルが変更されたのかが見れたりする。

例えば、

$git log --name-only

を実行すれば、各コミット毎に、変更されたファイルが表示される。こんな感じ。

commit 338d9dde08a13d981e8fd71a973f793be372078d
Author: ton <tonton1517@gmail.com>
Date:   Mon Nov 11 18:38:20 2013 +0900

    initial commit.

README

しかし、マージコミットの中にdiffがあると、これが表示されない。以下再現と解決策。

再現

現在はmasterブランチで上記のログの状態。コミットは1つだけでREADMEという空のファイルだけ。

1. 新しいブランチを作って適当にコミットする。

$git checkout -b test-branch
$echo "test" > TEST_FILE
$git add TEST_FILE
$git commit -m "2nd commit."
$git log --name-only

commit 15a306825681b574470780781d105a50cb95b895
Author: ton <tonton1517@gmail.com>
Date:   Mon Nov 11 18:47:00 2013 +0900

    2nd commit.

TEST_FILE

commit 338d9dde08a13d981e8fd71a973f793be372078d
Author: ton <tonton1517@gmail.com>
Date:   Mon Nov 11 18:38:20 2013 +0900

    initial commit.

README

2. masterブランチに戻って、–no-ffでマージする(コミットはしない)

マージコミットを作りたいので、--no-ffにし、まだコミットさせたくないので--no-commitオプションをつける。 これはマージ時にコンフリクトが発生したときと同じ状況。

$git checkout master
$git merge test-branch --no-ff --no-commit

3. マージ途中で新しいファイルを追加し、マージコミットを行う

$echo "this diff is in merge commit" > ONLY_MERGE_COMMIT_FILE
$git add ONLY_MERGE_COMMIT_FILE
$git commit -m "merge"

4. ログを見る

$git log --name-only

commit b39fe10881caec2cdc0b6b98a3ad23ed9aa19239
Merge: 338d9dd 15a3068
Author: ton <tonton1517@gmail.com>
Date:   Mon Nov 11 18:57:59 2013 +0900

    merge

commit 15a306825681b574470780781d105a50cb95b895
Author: ton <tonton1517@gmail.com>
Date:   Mon Nov 11 18:47:00 2013 +0900

    2nd commit.

TEST_FILE

commit 338d9dde08a13d981e8fd71a973f793be372078d
Author: ton <tonton1517@gmail.com>
Date:   Mon Nov 11 18:38:20 2013 +0900

    initial commit.

README

マージコミットだけファイルが表示されない。。。

解決策

git help logでヘルプとにらめっこしてたら-cオプションを見つけた。

-c
    With this option, diff output for a merge commit shows the differences from each of the parents to the
    merge result simultaneously instead of showing pairwise diff between a parent and the result one at a
    time. Furthermore, it lists only files which were modified from all parents.
$git log --name-only -c

commit b39fe10881caec2cdc0b6b98a3ad23ed9aa19239
Merge: 338d9dd 15a3068
Author: ton <tonton1517@gmail.com>
Date:   Mon Nov 11 18:57:59 2013 +0900

    merge

ONLY_MERGE_COMMIT_FILE

commit 15a306825681b574470780781d105a50cb95b895
Author: ton <tonton1517@gmail.com>
Date:   Mon Nov 11 18:47:00 2013 +0900

    2nd commit.

TEST_FILE

commit 338d9dde08a13d981e8fd71a973f793be372078d
Author: ton <tonton1517@gmail.com>
Date:   Mon Nov 11 18:38:20 2013 +0900

    initial commit.

README

表示された。

普通マージ時に発生したdiffなんて表示したくないって理由でこうなってるんだろうけど、 今回の再現のようにマージコミットの中にしかdiffがないファイルがあって、ログを解析しててめっちゃ困ってた。

2013/11/11

プロフィールアイコン

ton

何でもやりたいエンジニア

趣味でFlash作って遊んでいたらプログラマーになってしまいました。

仕事ではSNSの運用したり、ゲーム作ったり、webサービス作ったり、アプリ作ったり、色々してます。