修正されたファイルと追加されたファイルのテスト
前のステップでは、git show --name-status
と git diff-tree --name-status
が追加されたファイルに対して A
を表示する方法を見ました。では、ファイルが修正された場合にこれらのコマンドがどのように動作するかを見てみましょう。
まず、~/project/my-time-machine
ディレクトリにいることを確認してください。
message.txt
ファイルに別の行を追加しましょう。
echo "Hello again, Future Me" >> message.txt
>>
演算子は、テキストを既存のファイルに追加します。
では、リポジトリの状態を確認しましょう。
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 はファイルが modified
(修正された)ことを正しく識別しています。
では、この変更をステージングしましょう。
git add message.txt
そして、再度状態を確認しましょう。
git status
出力は、変更がステージングされたことを示すはずです。
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: message.txt
コンテンツを追加したにもかかわらず、Git はこれを modified
ファイルとして追跡していることに注意してください。なぜなら、このファイルは前のコミットですでに存在していたからです。
最後に、この変更をコミットしましょう。
git commit -m "Add another message to the future"
コミットが確定したことを示す出力が表示されるはずです。
[master a1b2c3d] Add another message to the future
1 file changed, 1 insertion(+)
これで新しいコミットができました。最新のコミット (HEAD
) に対して git show --name-status
を使用して、状態を確認しましょう。
git show --name-status HEAD
出力は次のようになります。
commit a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6q7r8s9 (HEAD -> master)
Author: Jane Doe <[email protected]>
Date: Mon Aug 7 10:05:00 2023 +0000
Add another message to the future
M message.txt
message.txt
の前に M
があることに注意してください。これは、このコミットでファイルが Modified
(修正された)ことを示しています。
同様に、git diff-tree -r --name-status HEAD
を使用しても、M
ステータスが表示されます。
A
(追加)と M
(修正)のこの区別は、Git でのファイルの履歴を理解するために重要です。A
はそのコミットでファイルが作成されたことを意味し、M
はファイルが以前から存在し、その内容が変更されたことを意味します。