git rebase -i
を使ってコミット履歴いじっちゃいましょうって話です。
コミットコメントのミスってありますよね?
コミットコメントにルールを設けていない環境のためガンガンコミットしちゃっているのですが、この間こんなミスをしました。
PHPのプロジェクトでimgタグの発行ライブラリを整理していたため、コメントに
git commit -m "画像ライブラリの整理"
と書こうとしたところ
git commit -m "画像ライブラリの生理"
と打っていて間違いに気づかずそのままpush。
数分後、プルリクを送ってようやくミスに気づきました。
女性もいるプロジェクトですので流石にまずいです。笑えません。
今回使用しているブランチは私しか触らないため、git push -f も使えます。
まずは、git commit --amend を試みます。
git commit --amend
を用いて生理を整理に書き換えます
さぁpushだ!!!
・・・あれ・・・?rejectされるぞ。当然です。
ここでrebaseの出番です。
git rebase -i HEAD~2
と打つと下記のような表示になりました。
pick 8145f1c 画像libararyの整理
pick d90db4a 画像libararyの生理
# Rebase ed7420a..71a6940 onto ed7420a
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
#
# 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.
#
上のコミットを残したいので、下のコミットのpickをfixupに書き換え、:wqで保存し、脱出します。
そして、git push -f origin picture
のように打てばpush完了です!