高木のブログ

【Git】2つ以上前のコミットを修正する

2021/05/29

2つ以上前のコミットの間違いを見つけた時の修正方法

自分用結論メモ

$ git rebase -i HEAD~~

edit 253f7ad Add commit A
pick 779aa5d Add commit B

$ git commit --amend
$ git rebase --continue

手順

commit_A.txtのcommitをタイポした設定でコミット253f7ad Add commit Aを修正する

commit_A.txt
commmit A
commit_B.txt
commit B
$ git log --oneline -p
779aa5d (HEAD -> master) Add commit B
diff --git a/commit_B.txt b/commit_B.txt
new file mode 100644
index 0000000..df9c5fb
--- /dev/null
+++ b/commit_B.txt
@@ -0,0 +1 @@
+commit B
253f7ad Add commit A
diff --git a/commit_A.txt b/commit_A.txt
new file mode 100644
index 0000000..c2bb3c1
--- /dev/null
+++ b/commit_A.txt
@@ -0,0 +1 @@
+commmit A
$ git rebase -i HEAD~~

253f7ad Add commit Aのpickをeditに変更

edit 253f7ad Add commit A
pick 779aa5d Add commit B

# Rebase ea2f42d..779aa5d onto ea2f42d (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
# 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
# .       specified). Use -c <commit> to reword the commit message.
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#

commit_A.txtを修正する

$ vim commit_A.txt
$ git add commit_A.txt
$ git commit --amend
$ git rebase --continue
Successfully rebased and updated refs/heads/master.

参考

Git ふたつ以上前のコミットには commit —amend できないの? - Qiita


SNS でシェアする


ytkg

Written by ytkg, Twitter, GitHub