目的別に“安全/破壊的”を分けて最短コマンドだけ。失敗しても戻れるリカバリも最後に。
「戻る」の3つの意味を整理
- 見るだけ(安全): 一時的にその時点のコードを確認したい → detached HEAD でチェックアウト
- ブランチ自体を巻き戻す(破壊的): 現在のブランチ履歴を過去に合わせたい →
reset --hard - 取り消しを積む(安全): 過去の変更を“打ち消すコミット”を追加 →
revert
任意のコミットまで戻る
1) 見るだけ(安全)
# コミット一覧を見てハッシュを控える(7桁でOK)
git log --oneline --graph --decorate --all
# そのコミットの状態を一時的にチェックアウト(detached HEAD)
git switch --detach <commit-hash>
# その場から作業したいならブランチを切る
git switch -c trial-from-<short-hash>
2) ブランチを過去へ巻き戻す(破壊的・共有ブランチ注意)
# ローカル変更を破棄してブランチを指定コミットへ
git reset --hard <commit-hash>
# すでにリモートへpush済みなら歴史を書き換えるので注意
git push --force-with-lease origin <branch>
注意:
--forceではなく--force-with-lease推奨(他人の更新を誤上書きしにくい)。
3) 取り消しコミットを積む(安全)
# 単一コミットを打ち消す
git revert <commit-hash>
# 複数をまとめて(コミットは新しい順に自動適用。途中衝突したら解決→commit)
git revert --no-commit <oldest>^..HEAD
git commit -m "Revert changes from <oldest>..HEAD"
補助: 特定ファイルだけ過去から持ってくる(安全)
git restore --source=<commit-hash> -- path/to/file
任意のタグまで戻る
1) タグの状態を“見るだけ”(安全)
# タグ一覧
git tag --list
# タグ時点へ一時的に移動(detached HEAD)
git switch --detach <tag>
# そこから作業するならブランチを切る
git switch -c hotfix-from-<tag> <tag>
2) ブランチをタグへ巻き戻す(破壊的)
git reset --hard <tag>
git push --force-with-lease origin <branch>
どれを選ぶ?クイック判断
- 安全に確認だけ →
switch --detach - 個人ブランチでガッと戻したい →
reset --hard(pushは--force-with-lease) - 共有ブランチで履歴を汚さず取り消したい →
revert
作業前のワンアクション(超大事)
# 未コミットを一時避難
git stash push -m "wip before rollback"
# 取り出し
git stash pop
もしやらかしたら:リカバリ
# 直近のHEAD移動の足跡(ほぼ最後の砦)
git reflog
# 見つけた地点へ戻す
git reset --hard <hash-from-reflog>
最小チートシート
# コミットへ「見るだけ」
git switch --detach <hash>
# コミットへ「巻き戻し」
git reset --hard <hash> && git push --force-with-lease
# コミットを「打ち消す」
git revert <hash>
# タグからブランチを切る
git switch -c fix-issue <tag>
ネイティブっぽい言い回しメモ(英語で書くとき):
- 「過去のコミットに戻す」→ reset the branch to a previous commit
- 「取り消しコミットを積む」→ revert a commit
- 「タグから派生ブランチを切る」→ branch off from a tag


コメント