ホーム 格闘技 [Git] Summary of how to undo git add - Removing from the staging area

[Git] Summary of how to undo git add - Removing from the staging area

八木 麻衣 八木 麻衣 • 更新: • 公開: • 読了約 14 分
目次


nf44dcd2b3890

Introduction

Have you ever experienced this while working with Git?

# 間違えて追加してしまった...
git add .
git add .idea/
git add secret_config.py
やっぱり取り消したい!

In this article, I will explain how to undo git add for different situations.

Target Audience

  • Git beginners to intermediate users

  • Those who want to undo a file they accidentally added with git add

  • Those who want to understand how to operate the staging area

How git add works

First, let's understand what git add does.

作業ディレクトリ → [git add] → ステージングエリア → [git commit] → リポジトリ

git add adds files to the staging area. Undoing git add =
removing from the staging area

By situation: How to undo git add

Pattern 1: Undo only a specific file
# 特定のファイルのaddを取り消す
git reset HEAD ファイル名
例:secret_config.py を取り消す
git reset HEAD secret_config.py
例:.ideaディレクトリを取り消す
git reset HEAD .idea/

Action:

  • Removed from the staging area

  • The file itself remains (as is in the working directory)

  • Changes are also preserved

Pattern 2: Undo all git add operations
# 全てのaddを取り消す
git reset HEAD
または
git reset

Action:

  • The staging area becomes empty

  • All files will return to an unstaged state

  • Files and changes are preserved

Pattern 3: Completely remove files from Git management
# ファイルをGit管理から外す(ローカルファイルは残す)
git rm --cached ファイル名
例:.ideaディレクトリをGit管理から外す
git rm --cached -r .idea/
例:設定ファイルをGit管理から外す
git rm --cached config/secret.yml

Action:

  • Removed from the staging area

  • Will be removed from the Git repository in the next commit

  • Local files remain (thanks to the --cached option)

Note: This is an operation to "remove from Git management," so it will be reflected in the next commit.

Pattern 4: Completely delete files
# ファイルをGit管理から外し、ローカルからも削除
git rm ファイル名
例:不要なファイルを完全削除
git rm old_file.py

Warning: Local files will also be deleted! Use with caution.

Solutions for common cases

Case 1: Accidentally added .idea/ with add
# ステップ1:addを取り消す
git reset HEAD .idea/
ステップ2:.gitignoreに追加
echo ".idea/" >> .gitignore
ステップ3:.gitignoreをコミット
git add .gitignore
git commit -m "Add .idea to gitignore"
Case 2: Want to remove .idea/ that has already been committed from Git management
# ステップ1:Git管理から外す(ファイルは残す)
git rm --cached -r .idea/
ステップ2:.gitignoreに追加
echo ".idea/" >> .gitignore
ステップ3:変更をコミット
git add .gitignore
git commit -m "Remove .idea from Git tracking"
Case 3: Accidentally added sensitive information
# すぐにaddを取り消す
git reset HEAD config/secret.yml
.gitignoreに追加
echo "config/secret.yml" >> .gitignore
確認
git status

If you have already committed and pushed:

  • Change passwords or API keys immediately

  • Must be completely removed from Git history (explained in another article)

Case 4: Added everything with git add .
# 全てのaddを取り消す
git reset HEAD
必要なファイルだけ追加し直す
git add src/main.py
git add README.md
git commit -m "Add main files"

Options for git reset

There are several options for git reset.

git reset HEAD (default)
git reset HEAD ファイル名
  • Remove from staging area

  • Working directory remains unchanged

  • The safest option

git reset --soft HEAD
git reset --soft HEAD
  • Staging area remains as is

  • Undo commit only (do not use for undoing add)

git reset --hard HEAD
git reset --hard HEAD
  • Clear the staging area

  • Discard all changes in the working directory as well

  • Danger: All changes will be lost!

How to verify

Command to check the status of add:

# ステージングエリアの状態を確認
git status
詳細な差分を確認
git diff --cached

How to read git status:

$ git status

Changes to be committed: # ← ステージングエリア(add済み)
 (use "git reset HEAD <file>..." to unstage)
 modified: file1.py
 new file: file2.py

Changes not staged for commit: # ← 未ステージ(add未実施)
 modified: file3.py

Untracked files: # ← Git管理外
 file4.py

Practical examples

Example 1: After accidentally adding everything, commit only the necessary files
# 間違えて全部add
git add .
確認
git status
→ .idea/, pycache/, など不要なファイルもaddされている
全てリセット
git reset HEAD
必要なファイルだけadd
git add src/
git add README.md
git add requirements.txt
コミット
git commit -m "Add source code and documentation"
Example 2: Remove PyCharm configuration files from Git management
# 既にaddしてしまった場合
git reset HEAD .idea/
既にコミットしてしまった場合
git rm --cached -r .idea/
.gitignoreに追加
echo ".idea/" >> .gitignore
コミット
git add .gitignore
git commit -m "Stop tracking .idea directory"
Example 3: Undo multiple files at once
# 複数ファイルを取り消す
git reset HEAD file1.py file2.py file3.py
ワイルドカードも使える
git reset HEAD .log
git reset HEAD tests/.pyc

Summary

Command Purpose Safety git reset HEAD filename Undo add Safe git reset HEAD Undo all adds Safe git rm --cached filename Remove from Git management Caution git rm filename Delete file Dangerous git reset --hard HEAD Discard all changes Very dangerous

Basically, you can just use git reset HEAD filename!

Frequently Asked Questions

Q1: What is the difference between git reset HEAD and git rm --cached?

A:

  • git reset HEAD: Only undoes the add (no impact on the Git repository)

  • git rm --cached: Removes from Git management (reflected in the next commit)

Q2: What if I have already committed?

A: You cannot undo it with git reset HEAD. Use git revert or git reset --soft HEAD^, etc.

Q3: What if I notice immediately after accidentally adding?

A: You can undo it immediately with git reset HEAD filename. It is safe, so please feel free to run it.

Q4: I wrote it in .gitignore, but it still gets added

A: .gitignore does not affect files already under Git management. You need to remove them from management using git rm --cached.

Reference

  • Git Official Documentation - git-reset

  • Git Official Documentation - git-rm


同じカテゴリの記事

八木 麻衣
トレンドリサーチャー 八木 麻衣
プロフィールを見る →

全記事の事実確認を担当。一次情報・公式発表・公開資料の三点を確認できない場合は公開を見送る判断をします。

おすすめの関連記事