「設定ファイルを間違えてコミットしてしまった」 「デバッグコードを含めたままpushしてしまった」 「大きなファイルをコミットしてしまった」 「他のファイルは残したまま、特定のファイルだけ取り消したい」
Gitを使っていると、こんな「やってしまった!」という瞬間が必ずあります。
でも大丈夫。Gitには様々な取り消し方法があり、状況に応じて最適な方法を選べます。特に、特定のファイルだけを取り消すテクニックを知っていれば、他の変更を壊さずに問題を解決できます。
この記事では、コミットの取り消し方法を状況別に詳しく解説します。
状況別クイックガイド:どの方法を使うべき?

あなたの状況は?
まず、自分の状況を確認しましょう:
状況 | 使うコマンド | 影響範囲 |
---|---|---|
まだステージング前(add前) | git checkout or git restore | ローカルのみ |
ステージング済み(add後、commit前) | git reset or git restore --staged | ローカルのみ |
直前のコミットを修正 | git commit --amend | ローカルのみ |
コミット済み(push前) | git reset or git revert | ローカルのみ |
push済み | git revert (推奨) | リモートも影響 |
緊急度別の選択
今すぐ直したい(ローカルのみ):
# 最新コミットから特定ファイルを取り消し
git checkout HEAD~1 -- path/to/file.txt
git commit -m "Revert specific file"
安全に取り消したい(push済み):
# revertで安全に取り消し
git revert HEAD --no-commit
git reset HEAD path/to/keep.txt # 残したいファイル
git commit -m "Revert only specific files"
ステージング前の変更を取り消す(add前)
特定ファイルの変更を取り消す
方法1:git restore(推奨・Git 2.23以降)
# 特定ファイルを元に戻す
git restore path/to/file.txt
# 複数ファイルを指定
git restore file1.txt file2.txt
# ディレクトリ全体
git restore src/
# 対話的に選択
git restore -p file.txt
方法2:git checkout(従来の方法)
# 特定ファイルを元に戻す
git checkout -- path/to/file.txt
# 複数ファイル
git checkout -- *.txt
# すべての変更を取り消し(危険!)
git checkout -- .
変更内容を確認してから取り消す
# 変更内容を確認
git diff path/to/file.txt
# 対話的に部分的に取り消し
git checkout -p path/to/file.txt
# y: この変更を取り消す
# n: この変更を残す
# q: 終了
# a: このファイルの残りすべてを取り消す
ステージングを取り消す(add後、commit前)
特定ファイルのステージングを解除
方法1:git restore –staged(推奨)
# 特定ファイルのステージングを解除
git restore --staged path/to/file.txt
# 複数ファイル
git restore --staged file1.txt file2.txt
# パターンマッチ
git restore --staged "*.log"
# すべてのステージングを解除
git restore --staged .
方法2:git reset(従来の方法)
# 特定ファイルのステージングを解除
git reset HEAD path/to/file.txt
# 複数ファイル
git reset HEAD file1.txt file2.txt
# ディレクトリ
git reset HEAD src/
ステージング状態を確認
# ステージングされているファイルを確認
git status
# より詳細な情報
git status -s
# ステージングされた変更内容を確認
git diff --staged path/to/file.txt
直前のコミットを修正する(push前)
コミットから特定ファイルを除外
最新コミットから特定ファイルだけ取り消す:
# 方法1:前のバージョンに戻してコミット
git checkout HEAD~1 -- path/to/file.txt
git commit --amend --no-edit
# 方法2:リセットして再コミット
git reset HEAD~1 -- path/to/file.txt
git commit --amend --no-edit
コミットメッセージも修正
# ファイルを取り消してメッセージも変更
git checkout HEAD~1 -- unwanted-file.txt
git commit --amend -m "新しいコミットメッセージ"
間違えて追加したファイルを完全に削除
# コミットとファイルシステムから削除
git rm --cached sensitive-file.txt
git commit --amend --no-edit
# ファイルは残すがコミットから除外
git reset HEAD~1 sensitive-file.txt
git commit --amend --no-edit
過去のコミットから特定ファイルを取り消す
特定のコミットから特定ファイルを復元
# 特定コミットの特定ファイルを取得
git checkout <commit-hash> -- path/to/file.txt
# 例:3つ前のコミットからファイルを復元
git checkout HEAD~3 -- config/settings.json
# 特定のタグやブランチから復元
git checkout v1.0.0 -- README.md
git checkout develop -- src/main.js
複数のコミットから選んで取り消す
# コミット履歴を確認
git log --oneline -- path/to/file.txt
# 特定のコミットを選んで復元
git checkout abc1234 -- path/to/file.txt
# 複数ファイルを異なるコミットから復元
git checkout commit1 -- file1.txt
git checkout commit2 -- file2.txt
git revert を使った安全な取り消し(push済み対応)
特定ファイルだけをrevert
# 方法1:一時的に全体をrevertして、特定ファイルだけ適用
git revert HEAD --no-commit
git reset HEAD file-to-keep.txt # 残したいファイル
git commit -m "Revert only specific files"
# 方法2:特定ファイルの変更だけを逆適用
git show HEAD -- path/to/file.txt | git apply -R
git add path/to/file.txt
git commit -m "Revert changes to specific file"
複数コミットから特定ファイルをrevert
# 範囲指定でrevert
git revert HEAD~3..HEAD --no-commit
git reset HEAD files-to-keep/*
git commit -m "Revert specific files from multiple commits"
git reset の使い方(3つのモード)

reset の3つのモード理解
# --soft:コミットだけ取り消し(変更はステージングに残る)
git reset --soft HEAD~1
# --mixed(デフォルト):コミットとステージングを取り消し
git reset HEAD~1
# --hard:すべて取り消し(変更も消える!危険)
git reset --hard HEAD~1
特定ファイルだけreset
# 特定ファイルを特定のコミット状態に戻す
git reset HEAD~2 -- path/to/file.txt
# 複数ファイルを異なる状態に
git reset HEAD~1 -- file1.txt
git reset HEAD~3 -- file2.txt
# その後コミット
git commit -m "Reset specific files to previous states"
実践的な使用例
ケース1:パスワードファイルを誤ってコミット
# 状況:.envファイルをコミットしてしまった(まだpush前)
# 1. コミットから削除
git rm --cached .env
git commit --amend --no-edit
# 2. .gitignoreに追加
echo ".env" >> .gitignore
git add .gitignore
git commit --amend --no-edit
# 3. 履歴からも完全に削除(必要な場合)
git filter-branch --force --index-filter \
"git rm --cached --ignore-unmatch .env" \
--prune-empty --tag-name-filter cat -- --all
ケース2:大きなファイルをコミットしてpushできない
# 状況:100MB超のファイルをコミット、GitHubにpushできない
# 1. コミットから削除
git rm --cached large-file.zip
git commit --amend --no-edit
# 2. Git LFSを使用するか、別の方法で管理
git lfs track "*.zip"
git add .gitattributes
git add large-file.zip
git commit --amend
ケース3:間違ったブランチにコミット
# 状況:masterに直接コミットしてしまった
# 1. コミットを取り消し(ただし変更は残す)
git reset --soft HEAD~1
# 2. 正しいブランチに切り替え
git checkout -b feature/correct-branch
# 3. 再度コミット
git commit -m "正しいメッセージ"
インタラクティブrebaseで複雑な編集
複数コミットから特定ファイルを整理
# 過去5コミットを編集
git rebase -i HEAD~5
# エディタで編集したいコミットを"edit"に変更
# pick abc1234 -> edit abc1234
# 各コミットで特定ファイルを修正
git checkout HEAD~1 -- unwanted-file.txt
git commit --amend --no-edit
git rebase --continue
コミットの分割
# 大きなコミットを分割
git rebase -i HEAD~3
# 分割したいコミットを"edit"に
git reset HEAD~1 # コミットを取り消し
git add file1.txt
git commit -m "Change 1"
git add file2.txt
git commit -m "Change 2"
git rebase --continue
トラブルシューティング
取り消し操作を取り消す(やり直し)
# reflogで過去の状態を確認
git reflog
# 特定の状態に戻す
git reset --hard HEAD@{2}
# または特定のコミットに
git reset --hard abc1234
コンフリクトが発生した場合
# revert中のコンフリクト
git status # コンフリクトファイルを確認
# ファイルを編集してコンフリクト解決
git add resolved-file.txt
git revert --continue
# またはrevertを中止
git revert --abort
間違えてresetしてしまった場合
# 消えたコミットを探す
git reflog
git fsck --lost-found
# 復元
git checkout <lost-commit-hash>
# または
git cherry-pick <lost-commit-hash>
ベストプラクティス
プッシュ前後で使い分ける
push前:自由に履歴を書き換えOK
# reset、rebase、amendを活用
git reset --soft HEAD~1
git commit --amend
git rebase -i
push後:履歴を保持して修正
# revertを使用
git revert HEAD
# 絶対にforce pushしない(共同作業を壊す)
エイリアスで効率化
# よく使うコマンドをエイリアス化
git config --global alias.unstage 'reset HEAD --'
git config --global alias.undo 'checkout --'
git config --global alias.amend 'commit --amend --no-edit'
# 使用例
git unstage file.txt
git undo file.txt
git amend
コミット前のチェックリスト
# コミット前に必ず確認
git status # 意図しないファイルがないか
git diff --staged # 変更内容は正しいか
git log --oneline -5 # コミット履歴は適切か
よくある質問と回答
Q1:git resetとgit revertの使い分けは?
A:
- reset:履歴を書き換える。push前の個人作業で使用
- revert:新しいコミットで打ち消す。push後や共同作業で使用 チーム開発ではrevertが安全です。
Q2:間違えてgit reset –hardしてしまった!
A: git reflog
で履歴を確認し、git reset --hard HEAD@{n}
で復元できます。ただし、ステージングされていない変更は復元できません。
Q3:特定のファイルだけ別のブランチから持ってきたい
A:
git checkout other-branch -- path/to/file.txt
これで他のブランチの特定ファイルだけを現在のブランチにコピーできます。
Q4:.gitignoreに追加したのに無視されない
A: すでに追跡されているファイルは無視されません。
git rm --cached file.txt
git commit -m "Remove tracked file"
これで追跡を解除してから.gitignoreが効きます。
Q5:コミットメッセージだけ変更したい
A:
# 最新のコミット
git commit --amend -m "新しいメッセージ"
# 過去のコミット
git rebase -i HEAD~n # 該当コミットをrewordに変更
まとめ:状況に応じた最適な取り消し方法を選ぼう
Gitのコミット取り消しは、状況に応じて適切な方法を選ぶことが重要です。
覚えておくべき基本原則:
- push前は自由、push後は慎重に
- push前:reset、amend、rebase OK
- push後:revert推奨、force push禁止
- 特定ファイルの操作
git restore
:作業ツリーの変更取り消しgit reset
:ステージング/コミット取り消しgit checkout
:特定バージョンの復元
- 安全第一
- 不安なときは先にバックアップ
git stash
で一時保存- reflogがあれば大抵復元可能
- チーム開発では
- 履歴の書き換えは避ける
- revertで透明性を保つ
- コミュニケーションを大切に
これらのテクニックをマスターすれば、Gitでのミスを恐れることなく、自信を持って開発を進められます!
コメント