という名前のファイルがあるとしましょうa.txt
。それをステージング領域に追加してから、変更します。追加したときの状態に戻すにはどうすればよいですか?
回答:
git checkout a.txt
git restore a.txt
を入力すると、Gitがこれを通知しますgit status
。
Git 2.23より前:
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: a
#
# Changed but not updated:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified: a
#
Git 2.23以降:
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: a
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: a
--
ステータスが言うように動作しました。
git checkout -- a.txt
このページの他の回答にはがなく--
、混乱を招きました。
これは、Gitが入力したときに通知する内容ですgit status
。
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: a
#
# Changed but not updated:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified: a
#
ステージングされたファイルのステージング解除
次の2つのセクションでは、ステージング領域と作業ディレクトリの変更を操作する方法を示します。良い点は、これら2つの領域の状態を判別するために使用するコマンドが、それらへの変更を元に戻す方法も通知することです。たとえば、2つのファイルを変更し、それらを2つの別々の変更としてコミットしたいが、誤ってgit add *と入力し、両方をステージングしたとします。2つのうちの1つをどのようにアンステージできますか?gitstatusコマンドは次のことを思い出させます。
$ git add *
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
renamed: README.md -> README
modified: CONTRIBUTING.md
「変更をコミットする」というテキストのすぐ下に、git reset HEAD ...を使用してステージを解除すると書かれています。それでは、そのアドバイスを使用して、CONTRIBUTING.mdファイルのステージングを解除しましょう。
$ git reset HEAD CONTRIBUTING.md
Unstaged changes after reset:
M CONTRIBUTING.md
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
renamed: README.md -> README
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: CONTRIBUTING.md
コマンドは少し奇妙ですが、機能します。CONTRIBUTING.mdファイルは変更されていますが、再びステージングされていません。