適用された変更の Git ステータスを確認する
このステップでは、Git の stash
コマンドが作業ディレクトリとどのように相互作用するか、およびスタッシュを適用した後のリポジトリの状態を確認する方法を調べます。
まず、プロジェクトディレクトリにいることを確認しましょう。ターミナルを開き、my-time-machine
ディレクトリに移動します。
cd ~/project/my-time-machine
次に、新しいファイルを作成し、コミットされていない変更をシミュレートするためにいくつかの内容を追加しましょう。
echo "This is a new feature." > feature.txt
echo "Adding some more content." >> message.txt
これで、新しいファイル feature.txt
を作成し、既存の message.txt
を変更しました。git status
を使用して、Git がこれらの変更をどのように認識しているかを見てみましょう。
git status
feature.txt
が追跡されていないこと、および 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
Untracked files:
(use "git add <file>..." to include in what will be committed)
feature.txt
no changes added to commit (use "git add" and/or "git commit -a")
次に、これらの変更をスタッシュしましょう。スタッシュは、不完全な変更をコミットせずに、現在の作業を一時的に退避させて別の作業に切り替えることができるようにする機能です。
git stash save "Work in progress"
スタッシュが保存されたことを確認する出力が表示されるはずです。
Saved working tree and index state On master: Work in progress
これで作業ディレクトリはクリーンになり、変更を加えていないかのように見えるはずです。これを git status
で確認できます。
git status
出力はクリーンな作業ディレクトリを示すはずです。
On branch master
nothing to commit, working tree clean
次に、先ほど作成したスタッシュを適用しましょう。スタッシュを適用すると、スタッシュされた変更が作業ディレクトリに戻ります。
git stash apply
変更が適用されたことを示す出力が表示されるはずです。
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
Untracked files:
(use "git add <file>..." to include in what will be committed)
feature.txt
no changes added to commit (use "git add" and/or "git commit -a")
Dropped refs/stash@{0} (a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6q7r8s9)
最後に、git status
を再度確認して、スタッシュを適用した後の作業ディレクトリの状態を見てみましょう。
git status
出力は、スタッシュからの変更が作業ディレクトリに戻り、変更されたファイルと追跡されていないファイルとして表示されることを示すはずです。これは、git stash apply
が変更を自動的にステージまたはコミットせずに戻すことを確認します。