変更されていないファイルをテストする
前のステップで、ファイルが変更された場合の git status
と git diff
の動作を見ました。では、変更がない状態でこれらのコマンドを実行するとどうなるでしょうか?
確認してみましょう!~/project/my-time-machine
ディレクトリにいることを確認します。
まず、git status
を実行します。
git status
前回のステータス確認以来、何も変更を加えていないため、前と同じ出力が表示され、message.txt
が変更されているがステージングされていないことが示されます。
On branch master
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: message.txt
no changes added to commit (use "git add" and/or "git commit -a")
次に、再度 git diff
を実行してみましょう。
git diff
前と同じ差分出力が表示され、現在のファイルと最後のコミットとの違いが示されます。
diff --git a/message.txt b/message.txt
index a1b2c3d..e4f5g6h 100644
--- a/message.txt
+++ b/message.txt
@@ -1 +1,2 @@
Hello, Future Me
+P.S. Hope you're doing well!
これにより、git status
と git diff
は、さらに変更を加えずにコマンドを何度実行しても、最後のコミットに対する作業ディレクトリの現在の状態を表示することが確認できます。
次に、git add
を使用して message.txt
に加えた変更をステージングしましょう。
git add message.txt
再度 git status
を実行します。
git status
出力は、変更が現在ステージングされていることを示すように変わります。
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: message.txt
git status
が「Changes to be committed」を表示していることに注意してください。これは、変更がステージングエリアにあり、次のコミットの準備ができていることを意味します。
では、今度は git diff
はどうなるでしょうか?試してみましょう。
git diff
今回は、git diff
は何も出力しません。なぜでしょうか?git diff
を引数なしで実行すると、作業ディレクトリとステージングエリアを比較するからです。先ほど変更をステージングエリアに追加したので、作業ディレクトリとステージングエリアは同じになっています。
ステージングエリアと最後のコミットとの差分を見るには、git diff --staged
を使用します。試してみましょう。
git diff --staged
これにより、現在ステージングエリアにある変更の差分が表示されます。これは、追加した行です。
diff --git a/message.txt b/message.txt
index a1b2c3d..e4f5g6h 100644
--- a/message.txt
+++ b/message.txt
@@ -1 +1,2 @@
Hello, Future Me
+P.S. Hope you're doing well!
git diff
(作業ディレクトリ vs. ステージングエリア)と git diff --staged
(ステージングエリア vs. 最後のコミット)の違いを理解することは、Git の重要な概念です。これにより、コミットする前に変更を管理するのに役立ちます。
差分表示が表示された場合は、q
を押して終了します。