過去のコミットを編集したい

過去のコミットを編集したい
過去にコミットしたものの内容を変更したい場合の方法を備忘録としてこちらに残します。

過去にコミットしたFix CookingMemoryのカラム名の変更というコミットの内容を変更したい場合
Image from Gyazo

修正したいコミットはHEADから1つ前なので、+1して以下のように実行
git rebase -i HEAD~[戻したいコミット数+1]

$ higmonta@higuchimiyukiyuunoMacBook-Pro fishing_cooking % git rebase -i HEAD~2
Stopped at 2815474...  Fix CookingMemoryのカラム名の変更
You can amend the commit now, with

  git commit --amend 

Once you are satisfied with your changes, run

  git rebase --continue

下記のようになるので、編集したいコミットのpickeditに変えて、保存する。

pick e2f81c9 Fix CookingMemoryのカラム名の変更
pick a86abca Fix フォーム画面の記述のリファクタリング

# Rebase 238f49c..a86abca onto 238f49c (2 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 <commit> = like "squash", but discard this commit's log message
"~/workspace/fishing_cooking/.git/rebase-merge/git-rebase-todo" 27L, 1219B

編集したいファイルを編集する。
ちなみに編集後は、以下のようになります、

Image from Gyazo

下記のようにgit addを実行

$ higmonta@higuchimiyukiyuunoMacBook-Pro fishing_cooking % git add .

下記のようにgit commit --amendを実行

$ higmonta@higuchimiyukiyuunoMacBook-Pro fishing_cooking % git commit --amend
[detached HEAD e2f81c9] Fix CookingMemoryのカラム名の変更
 Date: Sun Apr 17 03:01:57 2022 +0900
 5 files changed, 12 insertions(+), 7 deletions(-)
 create mode 100644 db/migrate/20220416175212_change_cooking_memory_fish_column_to_fish_name.rb

ちなみに、この段階で以下のようになっています。

Image from Gyazo

下記のようにgit rebase --continueを実行
ちなみに、この段階で以下のようになっています。

Image from Gyazo

参考記事

[git]特定のコミットの内容を修正する - dackdive's blog