29.

git rm [-r --cached] の取り消し方法

編集
この記事の要点
  • git rm はファイルを削除 + ステージするコマンド
  • git rm --cachedGit の管理から外す(ファイル自体は残す)
  • 取り消し方法(コミット前): git restore --staged file + git restore file
  • 取り消し方法(コミット後): git revert または git reset
  • 完全に手戻したい場合: git checkout HEAD -- file

 

git rm の挙動

コマンド動作
git rm fileワーキングディレクトリから削除 + ステージング
git rm --cached fileGit 管理から外すだけ(ファイル自体は残る)
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+ 新コマンド)

従来 checkoutreset で行っていた取り消し操作を、より直感的にした新コマンド:

# ステージング取り消し (旧: 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 に追加

関連記事

編集
Post Share
子ページ

子ページはありません

同階層のページ
  1. 用語一覧
  2. エラー一覧
  3. git本体のインストール(Linux)
  4. Linuxサーバーへのgit導入とクライアントのセットアップ
  5. リモートリポジトリをローカルリポジトリとしてクローンする方法
  6. リモートとローカルのリポジトリを同期(pull)する方法
  7. 設定の確認
  8. gitユーザー名とemailの設定
  9. リモートリポジトリの作成
  10. ローカルリポジトリの作成
  11. 新規ファイル/ディレクトリをインデックスに登録
  12. インデックスの登録状態を確認
  13. ローカルリポジトリの変更をコミット
  14. コミット履歴の確認
  15. クライアントからリモートリポジトリの接続設定、確認、削除
  16. リポジトリへのプッシュ
  17. リモートリポジトリからクライアントへのSSHクローン
  18. リモートとローカルの差分表示
  19. バージョンの確認
  20. プロキシの設定
  21. ローカルをリモートリポジトリの状態に戻す
  22. ブランチの作成, 一覧表示, 切り替え
  23. ブランチのマージと削除
  24. リベース
  25. .gitignoreの書き方
  26. .gitignoreの設定が反映されない場合
  27. 特定のファイルをgitの管理から外す方法
  28. 参照(ORIG_HEAD, HEAD, FETCH_HEAD)
  29. git rm [-r --cached] の取り消し
  30. 一部のディレクトリ/ファイルのみをリポジトリから復元する方法
  31. ローカルとリモートリポジトリの有無を同期
  32. pushの取消し方法
  33. マージツールの起動方法
  34. Gitで「MERGING」の状態a