git diff コマンド

Git 基本操作Git 基本操作


git diff コマンドはファイルの相違を比較します。つまり、ファイルのステージングエリアとワークツリーの差分を比較します。

git diff コマンドは、ステージングエリアに書き込まれたファイルと、変更されたがまだステージングエリアに書き込まれていないファイルの違いを表示します。

git diff には主な利用シーンが2つあります。

  • ステージングされていない変更:git diff
  • ステージングされた変更を表示:git diff --cached
  • ステージング済みと未ステージングのすべての変更を表示:git diff HEAD
  • diff 全体ではなく要約を表示:git diff --stat

ステージングエリアとワークツリーの差分を表示:

$ git diff [file]

ステージングエリアと前回のコミット(commit)の差分を表示:

$ git diff --cached [file]
或
$ git diff --staged [file]

2つのコミット間の差分を表示:

$ git diff [first-branch]...[second-branch]

hello.php ファイルに以下の内容を入力します:

<?php
echo 'サンプル:www.example.com';
?>

git status を使用して状態を確認します:

$ git status -s
A  README
AM hello.php
$ git diff
diff --git a/hello.php b/hello.php
index e69de29..69b5711 100644
--- a/hello.php
+++ b/hello.php
@@ -0,0 +1,3 @@
+<?php
+echo '初心者チュートリアル:www.example.com';
+?>

git status は、前回のコミット以降の変更またはステージングされた変更を表示します。一方、git diff はこれらの変更が具体的に何であるかを一行ずつ表示します。

次に、以下を見てみましょう。git diff --cachedの実行結果:

$ git add hello.php
$ git status -s
A  README
A  hello.php
$ git diff --cached
diff --git a/README b/README
new file mode 100644
index 0000000..8f87495
--- /dev/null
+++ b/README
@@ -0,0 +1 @@
+# サンプル Git テスト
diff --git a/hello.php b/hello.php
new file mode 100644
index 0000000..69b5711
--- /dev/null
+++ b/hello.php
@@ -0,0 +1,3 @@
+<?php
+echo '初心者チュートリアル:www.example.com';
+?>


Git 基本操作Git 基本操作

その他の拡張