ゆずめも

メモ的なブログです。主に勉強した事について書いてます。

コミットメッセージを変更する - rebase編

amend編

rebase編を読む前に、先にamend編をお読みください

コミットメッセージを変更する - amend編 - Yuzu-pon Memo


準備

もう一つのパターン説明のために、2つのコミットを追加します

$ echo "hoge" >> readme.txt 
$ git add readme.txt
$ git commit -m "hoge追加"
$ echo "hogehoge" >> readme.txt 
$ git add readme.txt
$ git commit -m "hogehoge追加"

これでコミットが2つできました

$ git log --oneline 
9859ba9 hogehoge追加
0c1b4b9 hoge追加
6e65c18 readme.txt追加

これで準備OK

修正目標

hoge追加」を「readme.txtにhoge追加」に修正したいと思います

修正

前回の記事ではamendで修正しましたが
amendは直前のコミットを修正するオプションなので

$ git commit --amend "readme.txtにhoge追加"

としてしまうと「hogehoge追加」が「readme.txtにhoge追加」になってしまいます

なので今回はamendではなく、rebaseを使いたいと思います

$ git rebase -i head~2

git rebaseコマンドを -i (インタラクティブモード)で使う
headから2つ、って感じの意味です

実行すると以下の画面が表示されます

pick 0c1b4b9 hoge追加                                                           
pick 9859ba9 hogehoge追加

# Rebase 6e65c18..9859ba9 onto 6e65c18
#
# 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.
#
# Note that empty commits are commented out

英語で書いてありますが
r, rewordを使用します


変えたいコミット(今回場合hoge追加)の「pick」を「r」に変更します
「r」に変更し、エディタを保存して終了すると、もう一度エディタが起動します

ここでメッセージを書き換えて、保存して終了すると
コミットのメッセージが書き換わります

$ git log --oneline 
e45c676 hogehoge追加
64f5eb1 readme.txtにhoge追加
6e65c18 readme.txt追加