Gitで本番環境をひとつ前のコミットに戻す方法

本番サーバーでmaster originをpullしたところ、何らかの不具合が発生してしまいました。急いで元の状態に戻すときの手順の参考に!

以下の手順で本番環境を前のコミットに戻すことができます。

問題発生と復旧計画 本番サーバー master ⚠️不具合発生 ローカル環境 master リモート origin/master A B C 以前の安定状態 D 問題のコミット 復旧計画: コミットCの状態に戻す ローカル → リセット → リモートプッシュ → 本番pull

1. ローカル環境での操作

まず、ローカル環境でこれから行う作業:

  1. masterブランチに切り替え
  2. Gitの操作メニューから「Reset current branch to this Commit…」を選択
  3. 戻したい安定版のコミット(不具合発生前のコミット)を選択
  4. これでローカルのmasterブランチは指定したコミットの状態に戻ります
ローカル環境での復旧作業 ローカル環境 master リセット完了 本番サーバー master ⚠️不具合状態 リモート origin/master 未更新 Git操作メニュー Reset current branch to this Commit… Copy Commit Hash to Clipboard Copy Commit Subject to Clipboard A B C 選択中 D 問題のコミット ローカル操作 コミットCに戻すために リセットを実行
ローカル環境での復旧作業 ローカル環境 master Git操作メニュー Reset current branch to this Commit… Copy Commit Hash to Clipboard Copy Commit Subject to Clipboard A B C 選択中 D 問題のコミット ローカル操作 「Reset current branch to this Commit…」を選択し、 コミットCの状態にmasterブランチをリセット

2. リモートリポジトリへの反映

次に、変更をリモートに反映します:

# 必要であれば変更をコミット
git commit -m "Revert to previous stable version"

# リモートのmasterブランチに強制プッシュ
git push -f origin master
リモート更新と本番環境への反映 本番サーバー master ✅復旧完了 ローカル環境 master リセット済 リモート origin/master 更新済 1. git push -f 2. fetch & pull A B C 復旧した状態 D 削除されたコミット 復旧完了フロー 1. ローカル環境からリモートリポジトリへ強制プッシュ 2. 本番環境でfetchとpullを実行 3. 本番環境がコミットCの状態に復旧完了

強制プッシュ(-f)は通常の操作ではあまり推奨されませんが、今回のように問題を修正するためには必要な操作です。

3. 本番環境での反映

最後に、本番環境で以下のコマンドを実行:

# 最新の変更情報を取得
git fetch origin

# 変更を適用
git pull origin master

これで本番環境は指定したコミットの状態に戻り、不具合は解消されるはずです。

「Reset current branch」と「Rebase current branch」

Reset current branch(ブランチのリセット):

  • 現在のブランチの状態を指定したコミットに強制的に戻します
  • 指定したコミット以降の変更履歴が完全に削除されます(git reset --hardの場合)
  • 履歴が書き換えられるため、他の開発者と共有しているブランチでは注意が必要です
  • 主に「間違えた変更を完全に取り消したい」場合に使用します

Rebase current branch(ブランチのリベース):

  • コミット履歴を再構成する操作です
  • 指定したブランチやコミットの上に、現在のブランチのコミットを「積み直す」操作です
  • 元のコミットは残りませんが、各コミットの内容(変更内容)は維持されます
  • 履歴を整理してよりクリーンにする目的で使用します
  • ブランチの分岐点を変更することで、マージ時の競合を減らす目的でも使用されます

例えばコミットA→B→C→Dがあって、Cに戻したい場合:

  • Reset: Dが完全に削除され、Cの状態に戻ります
  • Rebase: DをCの内容に基づいて再適用し、新しいD’が作られます
Reset と Rebase の違い 初期状態 A B C D 問題のコミット HEAD (master) Reset操作後 A B C D HEAD (master) 問題のコミットDが削除される Rebase操作後 A B C D’ HEAD (master) コミットDがCの上に再適用される 比較 Reset: コミットを完全に削除し、履歴を巻き戻す Rebase: コミットを再構成し、変更内容は保持する

複数のコミットをまとめる方法(Git Graph)

まとめたいコミットの1つ前のコミットで右クリックします

SOURCE CONTROL Message (Ctrl+⏎ to commit on “main”) Publish Branch Branches: Show All Graph Description main コミット5回目 コミット4回目 コミット3回目 コミット2回目 コミット1回目

例えば、コミット3回目から5回目をまとめるとすると、2回目のところで右クリックします

Add Tag… Create Branch… Checkout… Cherry Pick… Revert… Drop… Merge into current branch… Rebase current branch on this Commit… Reset current branch to this Commit… Copy Commit Hash to Clipboard Copy Commit Subject to Clipboard

「Rebase current branch on this Commit…」をクリック

※現在作業中のブランチのベースを選択したコミットに変更するものです。つまり、現在のブランチの履歴を、指定したコミットを起点として再構築

Are you sure you want to rebase main (the current branch) on commit be37fa76 ? Launch Interactive Rebase in new Terminal Ignore Date i Yes, rebase Cancel

Launch Interactive Rebase in new Terminal」に✅をいれて、「Yes,rebase」

Interactive Rebase(インタラクティブリベース) – 通常のリベースよりも詳細な操作ができるモードです。各コミットに対して「pick(そのまま使用)」「squash(前のコミットと統合)」「edit(編集)」「reword(コミットメッセージを変更)」などの操作を選択できます。

in new Terminal(新しいターミナルで) – この操作を現在のウィンドウではなく、新しいターミナルウィンドウで開始します。これにより、リベース操作中も他の作業を続けられます。

Graph Description Date Author Commit main コミット5回目 21 Apr 2025 19:… LAPTOP-2C4PL… 3cc93f8c コミット4回目 21 Apr 2025 19:… LAPTOP-2C4PL… f2cbcb26 コミット3回目 21 Apr 2025 19:… LAPTOP-2C4PL… 33f8ae1d コミット2回目 21 Apr 2025 19:… LAPTOP-2C4PL… d6f56914 コミット1回目 21 Apr 2025 19:… LAPTOP-2C4PL… eb039a8b PROBLEMS OUTPUT DEBUG CONSOLE TERMINAL PORTS pick 33f8ae1 コミット3回目 pick f2cbcb2 コミット4回目 pick 3cc93f8 コミット5回目 # Rebase d6f5691..3cc93f8 onto d6f5691 (3 commands) # # Commands: # p, pick <commit> = use commit # r, reword <commit> = use commit, but edit the commit message # e, edit <commit> = use commit, but stop for amending # s, squash <commit> = use commit, but meld into previous commit # f, fixup [-c | -C] <commit> = like “squash” but keep only the previous commit’s log message; unless -c is used, in which case keep only this commit’s message; -c is same as -C but opens the editor # # x, exec <command> = run command (the rest of the line) using shell # b, break = stop here (continue rebase later with ‘git rebase –continue’) # d, drop <commit> = remove commit # l, label <label> = label current HEAD with a name # t, reset <label> = reset HEAD to a label # m, merge [-C <commit> | -c <commit>] <label> [# <oneline>] # create a merge commit using the original merge commit’s # message (or the oneline, if no original merge commit was git/rebase-merge/git-rebase-todo [unix] (19:58 21/04/2025) 1,1 Top Git Graph: R… Git Graph: R… Git Graph: R… Git Graph: R… Git Graph: R…

下記のように編集、まず i で「– INSERT –」インサートモードに変更

PROBLEMS OUTPUT DEBUG CONSOLE TERMINAL PORTS s 33f8ae1 コミット3回目 s f2cbc2 コミット4回目 pick 3cc93f8 コミット5回目

squash(s)は必ず前のコミットと結合するため最初のコミットは必ずpickである必要がある

※squashは押しつぶすという意味、つまり「複数のコミットを一つに押しつぶして統合する」という意味

修正が終わったら:ESC でインサートモードを終了:wq で保存して終了

※もし間違えたら、ESCキーinsertをぬけて:q!= 変更を保存せずに強制終了

下記の通り今度はコメントについての編集ができます。(git graphのグラフがまとまってなくて戸惑いますが、まだ途中なので、、)

修正が終わったら:ESC でインサートモードを終了:wq で保存して終了

Graph Description Date Author Uncommitted Changes (1) 21 Apr 2025 20:… * # This is a combination of 2 commits. # This is the 1st commit message: 21 Apr 2025 19:… LAPTOP-2C… main コミット5回目 21 Apr 2025 19:… LAPTOP-2C… コミット4回目 21 Apr 2025 19:… LAPTOP-2C… コミット3回目 21 Apr 2025 19:… LAPTOP-2C… コミット2回目 21 Apr 2025 19:… LAPTOP-2C… コミット1回目 21 Apr 2025 19:… LAPTOP-2C… PROBLEMS OUTPUT DEBUG CONSOLE TERMINAL PORTS # This is the commit message #2: コミット4回目 # This is the commit message #3: コミット5回目 # Please enter the commit message for your changes. Lines starting # with ‘#’ will be ignored, and an empty message aborts the commit. # # Date: Mon Apr 21 19:53:54 2025 +0900 # # interactive rebase in progress; onto d6f5691 # Last commands done (3 commands done): # squash f2cbc2 コミット4回目 # squash 3cc93f8 コミット5回目 # No commands remaining. # You are currently rebasing branch ‘main’ on ‘d6f5691’. # # Changes to be committed: # modified: index.html # git/COMMIT_EDITMSG [unix] (20:13 21/04/2025) 15,1 Bot :w█

下記の通りコミット3~5がまとまりました!

Graph Description main コミット3回目 コミット2回目 コミット1回目

参考サイト
https://qiita.com/tz77/items/cb6841d0f7ea591a0bfb