Table of Contents

git addコマンドを取り消したい場合は、git resetを使う。

  • git reset <file>: 特定のファイルをunstageする。
  • git reset: すべてのファイルをunstageする。

特定のファイルの例。

echo "test" >> file.txt
git add file.txt

git status
# Changes to be committed:
#  (use "git rm --cached <file>..." to unstage)
#        new file:   file.txt

git reset file.txt

git status
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#         file.txt

すべてのファイルの例。

git add .
git reset

さらに、git resetコマンドにはHEADまたはcommit IDを使うこともできる。

# 特定のファイル
git reset file.txt # "git reset HEAD file.txt"と同じ
git reset HEAD file.txt # "git reset file.txt"と同じ
git reset abc123 file.txt # commit ID "abc123"のfile.txtにリセット

# すべてのファイル
git reset
git reset HEAD
git reset abc123

git statusのメッセージによると、git rm --cached <file>も機能する。しかし、このコマンドは特定のファイルにのみ適用できる。