Works by

Ren's blog

@rennnosuke_rk 技術ブログです

【git】git diff にもっと詳しくなる

git diff

git diffは非常によく使うgitコマンドで、gitで管理されたファイルの差分をチェックするのに重宝しています。
ただ他にも使い方がないのか調べてみると、以外と使ったことがないオプションや引数指定の仕方がたくさんあったので、簡単にまとめてみました。

1. addしてないファイルの差分を表示

git diff

基本。デフォルト操作。
今いるブランチのHEADの状態と、アンステージング状態(addしてない)ファイルの最新状態の差分を表示します。

# hoge1.txt -> アンステージング状態
# hoge2.txt -> ステージング状態
$ git status
On branch master
Your branch is ahead of 'origin/master' by 2 commits.
  (use "git push" to publish your local commits)
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    modified:   hoge2.txt

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

    modified:   hoge1.txt


# hoge1.txtの差分表示
$ git diff

diff --git a/hoge1.txt b/hoge1.txt
index 1e3893b..84ec994 100644
--- a/hoge1.txt
+++ b/hoge1.txt
@@ -1,3 +1,4 @@
 hoge
 hoge
 piyo
+foo

addされたファイルの差分を表示

git diff --cached

git diffのステージング版。 今いるブランチのHEADの状態と、ステージング状態のファイルの最新状態の差分を表示します。

# hoge2.txtの差分表示
$ git diff

diff --git a/hoge2.txt b/hoge2.txt
index 2262de0..e42f4ce 100644
--- a/hoge2.txt
+++ b/hoge2.txt
@@ -1 +1,2 @@
 hoge
+fuga

addされたファイル・されていないファイル両方の差分を表示

git diff HEAD

ファイルの現在の状態(ワーキングツリー)と最新のコミット(HEAD)との差分を表示します。 git diffgit diff --cached両方の結果を合わせて見たいときに。

# ステージングされたファイル・されていないファイルがある
$ git status
On branch master
Your branch is ahead of 'origin/master' by 3 commits.
  (use "git push" to publish your local commits)
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    modified:   hoge1.txt

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

    modified:   hoge1.txt

# 両方の差分を見る
$ git diff HEAD
diff --git a/hoge1.txt b/hoge1.txt
index 84ec994..1e3893b 100644
--- a/hoge1.txt
+++ b/hoge1.txt
@@ -1,4 +1,3 @@
 hoge
 hoge
 piyo
-foo
diff --git a/hoge2.txt b/hoge2.txt
index e42f4ce..1a57997 100644
--- a/hoge2.txt
+++ b/hoge2.txt
@@ -1,2 +1,3 @@
 hoge
 fuga
+piyo

ブランチHEAD間の差分を表示

git diff [branch1] [branch1]

指定した二つのブランチbranch1``branch2の、同じファイルパスに存在するファイル同士全てを比較します。
ファイルの状態はブランチのHEAD時点のものです。
branch1からbranch2に至るまで、どのファイルのどの行が増えたか(消えたか)が表示されます。

# 二ブランチ間の比較
$ git diff branch1 branch2
diff --git a/hoge1.txt b/hoge1.txt
index 0ef7e93..1e3893b 100644
--- a/hoge1.txt
+++ b/hoge1.txt
@@ -1 +1,3 @@
+hoge
+hoge
 piyo
diff --git a/hoge2.txt b/hoge2.txt
new file mode 100644
index 0000000..9128c3e
--- /dev/null
+++ b/hoge2.txt
@@ -0,0 +1 @@
+fuga
diff --git a/hoge3.txt b/hoge3.txt
new file mode 100644
index 0000000..0ef7e93
--- /dev/null
+++ b/hoge3.txt
@@ -0,0 +1 @@
+piyo

別ブランチの別ファイル同士の差分を表示

git diff [branch1]:[file1] [branch2]:[file2]

ブランチbranch1中のfile1と、ブランチbranch2中のfile2を比較します。
これも、ファイルの状態はブランチのHEAD時点のものとなります。

$ git diff branch1:hoge1.txt branch2:hoge1.txt
diff --git a/branch1:hoge1.txt b/branch2:hoge1.txt
index 0ef7e93..1e3893b 100644
--- a/branch1:hoge1.txt
+++ b/branch2:hoge1.txt
@@ -1 +1,3 @@
+hoge
+hoge
 piyo

二つのコミット履歴間の差分を表示

git diff [commit1] [commit2]

二コミット間の差分を表示します。
commit1``commit2はコミットのSHAで指定可能ですが、HEADとかHEAD~とかHEAD^も指定できます。

# ブランチのHEADと、その一つ前のコミット時点との差分を表示
$ git diff HEAD~ HEAD 
diff --git a/hoge2.txt b/hoge2.txt
new file mode 100644
index 0000000..2262de0
--- /dev/null
+++ b/hoge2.txt
@@ -0,0 +1 @@
+hoge

# SHAで指定
$ git diff ebc2e989bc3eab80ae3e34790c01c591de4f2287 2969a1d4dc0face926b0d50343a9d506f71924f7
diff --git a/hoge2.txt b/hoge2.txt
deleted file mode 100644
index 2262de0..0000000
--- a/hoge2.txt
+++ /dev/null
@@ -1 +0,0 @@
-hoge

差分が存在するファイル名だけ表示

git diff --name-only

名前だけ表示。差分が存在するかどうかチェックするのに便利です。

# ファイル名表示
$ git diff --name-only
hoge1.txt

# 他のdiffパターンにも付加できる
$ git diff HEAD~ HEAD 
hoge2.txt

ローカルブランチとリモートブランチの差分を比較

git diff [remote][local]

ローカルとリモートの差分を見たい人用。
pull/pushするときなどに。

$ git diff master origin/master
diff --git a/hoge1.txt b/hoge1.txt
index 1e3893b..432a4b6 100644
--- a/hoge1.txt
+++ b/hoge1.txt
@@ -1,3 +1,2 @@
 hoge
 hoge
-piyo
diff --git a/hoge2.txt b/hoge2.txt
deleted file mode 100644
index 2262de0..0000000
--- a/hoge2.txt
+++ /dev/null
@@ -1 +0,0 @@
-hoge

差分の統計情報を表示

git diff --stat

変更された行や削除された行の数がファイルごとに表示されます。

# アンステージング状態のファイル
$ git status
On branch master
Your branch is ahead of 'origin/master' by 3 commits.
  (use "git push" to publish your local commits)
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

    modified:   hoge1.txt
    modified:   hoge2.txt

no changes added to commit (use "git add" and/or "git commit -a")

# どのファイルに、何行の修正・削除があったか
$ git diff --stat

 hoge1.txt | 1 -
 hoge2.txt | 1 +
 2 files changed, 1 insertion(+), 1 deletion(-)