タイトル: git rm [-r --cached] の取り消し
SEOタイトル: git rm [-r --cached] の取り消し方法
| この記事の要点 |
git rm はファイルを削除 + ステージするコマンド
git rm --cached はGit の管理から外す(ファイル自体は残す)
- 取り消し方法(コミット前):
git restore --staged file + git restore file
- 取り消し方法(コミット後):
git revert または git reset
- 完全に手戻したい場合:
git checkout HEAD -- file
|
git rm の挙動
| コマンド | 動作 |
git rm file | ワーキングディレクトリから削除 + ステージング |
git rm --cached file | Git 管理から外すだけ(ファイル自体は残る) |
git rm -r dir | ディレクトリ再帰的に削除 |
git rm --cached -r dir | ディレクトリを Git 管理から外す(ファイルは残る) |
git rm -f file | 変更があっても強制削除 |
シナリオ別の取り消し方法
シナリオ 1: git rm したがまだ commit していない
# 状況
$ git rm important.txt
rm 'important.txt'
$ git status
On branch main
Changes to be committed:
(use "git restore --staged ..." to unstage)
deleted: important.txt
# 取り消し (Git 2.23+)
$ git restore --staged important.txt # ステージから外す
$ git restore important.txt # ファイル復元
# 旧コマンド (Git 2.22 以前)
$ git reset HEAD important.txt
$ git checkout -- important.txt
シナリオ 2: git rm --cached したがまだ commit していない
# 状況: ファイル自体は残っているが Git 管理から外れた
$ git rm --cached config.local.json
rm 'config.local.json'
$ git status
On branch main
Changes to be committed:
deleted: config.local.json
Untracked files:
config.local.json # ← 残っている
# 取り消し: 再度 add するだけで OK
$ git add config.local.json
# または
$ git restore --staged config.local.json
シナリオ 3: commit してしまった後の取り消し
# 状況
$ git rm important.txt
$ git commit -m "remove important.txt"
# 方法 A: 直前のコミットだけ元に戻す (history 維持)
$ git revert HEAD
[main abc1234] Revert "remove important.txt"
# → 削除を打ち消す新しいコミットが作られる
# 方法 B: コミット自体を取り消す (history を書き換える)
$ git reset --soft HEAD~ # コミットだけ取り消し、変更はステージ済み
$ git reset HEAD~ # コミット + ステージ取り消し、変更はワーキング
$ git reset --hard HEAD~ # 完全に元の状態に (危険、変更も消える)
# 注意: reset は履歴を書き換えるので、push 済みなら revert を推奨
シナリオ 4: コミット後にファイルだけ復元したい(履歴は保持)
# 直前のコミットでファイルを復元
$ git checkout HEAD~ -- important.txt
# → important.txt を 1 つ前の状態に戻す(変更としてステージング)
# 履歴から特定のファイルを復元
$ git log --all --full-history -- important.txt # 履歴を確認
$ git checkout abc1234 -- important.txt # その commit の状態で復元
$ git commit -m "restore important.txt"
シナリオ 5: git rm --cached を取り消したい(push 済み)
# よくあるパターン: .gitignore に追加し忘れて誤って commit してしまった
$ git rm --cached config.local.json
$ git commit -m "untrack config.local.json"
$ git push
# 取り消し
$ git revert HEAD # 削除コミットを打ち消す
$ git push
# → 元の commit でファイルが復活
# または完全にこのファイルを履歴から削除(push 後でも残骸を消したい場合)
# → git filter-repo や BFG Repo-Cleaner(破壊的、要慎重)
git rm のよくあるユースケース
① 誤って commit したファイルを管理から外す
# 例: .env や IDE 設定ファイルを誤って commit
$ git rm --cached .env
$ git rm --cached -r .idea
$ echo ".env" >> .gitignore
$ echo ".idea/" >> .gitignore
$ git add .gitignore
$ git commit -m "untrack .env and .idea"
② ディレクトリ全体を削除
# ワーキングから削除 + ステージ
$ git rm -r legacy/
# 管理から外すだけ(ファイルは残す)
$ git rm --cached -r build/
③ 削除済みファイルをまとめて反映
# 既に手動で削除したファイルを git に反映
$ rm file1.txt file2.txt
$ git add -u # 削除も含めて全変更をステージ
# または
$ git rm $(git ls-files --deleted)
git restore(Git 2.23+ 新コマンド)
従来 checkout と reset で行っていた取り消し操作を、より直感的にした新コマンド:
# ステージング取り消し (旧: git reset HEAD )
$ git restore --staged file.txt
# ワーキングディレクトリの変更を破棄 (旧: git checkout -- file)
$ git restore file.txt
# 特定の commit から復元
$ git restore --source HEAD~ file.txt
# ステージとワーキング両方を取り消し
$ git restore --staged --worktree file.txt
取り消しコマンド早見表
| 状況 | コマンド |
| ステージング取り消し | git restore --staged |
| ワーキングの変更を破棄 | git restore |
| 直前のコミットを取り消す(履歴維持) | git revert HEAD |
| 直前のコミットを取り消す(履歴書換) | git reset HEAD~ |
| ファイルを履歴から復元 | git checkout -- |
| add した変更を取り消す | git restore --staged . |
注意点
- push 済みは revert: history 書き換え (reset) はリモートに push 済みなら避ける
- --hard は破壊的:
reset --hard は復元不可(reflog から救出は可能だが面倒)
- reflog で救済:
git reflog で過去の HEAD 履歴を見て git reset --hard ORIG_HEAD
- .gitignore は既追跡ファイルには効かない: 先に
git rm --cached で外してから .gitignore に追加
関連記事