ステージングを取り消す(INDEXを戻す)
あるファイルのステージングを取り消す
vi test.txt # (1) test.txtを修正する
git add test.txt # (2) test.txtをステージングする
git reset HEAD test.txt # (3) 上のステージングを取り消す(INDEXをHEADに戻す)
# test.txtは修正されたまま(INDEXがHEADに戻っただけで、ワーキングツリーはそのまま)
- git reset HEAD の代わりに git reset --mixed HEAD と省略せずに指定してもいい
まとめて複数のファイルのステージングを取り消す
ステージングしたファイルの一覧を取得する
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: 1.txt
modified: 2.txt
modified: 3.txt
$ git diff --cached --name-only
1.txt
2.txt
3.txt
ステージングしたファイルの一覧をまとめて取り消す
PowerShell> git reset HEAD (git diff --cached --name-only)
Bash> git reset HEAD $(git diff --cached --name-only)
修正したファイル戻す
git checkout test.txt # test.txtは(1)で修正される前の状態に戻った。
一度もコミットしてない状態でADDしたファイルのステージングを取り消す
git rm --cached test.txt
- 一度もコミットしていないと、git reset HEADしようとしても以下のようなエラーが出る。
fatal: ambiguous argument 'HEAD': unknown revision or path not in the working tree.