Motomichi Works Blog

モトミチワークスブログです。その日学習したことについて書いている日記みたいなものです。

gitその0001-03 git rebase入門 いくつか前のコミットメッセージを修正する

はじめに

いくつか前のcommitメッセージを修正する方法について書いています。

vimの操作方法についてはこのページでは細かくは触れません。

gitのコミットidはご自身の環境として読み替えてください。

git commit –amend についても触れません。

準備

以前の記事「gitその0001-01 git rebase入門 indexと準備 - Motomichi Works Blog」のとおりrebase_tutorialリポジトリを準備して、masterブランチとhogeブランチを作成しましたので、それを使います。

hogeブランチで作業してみます。

git checkout hoge

2つ前と3つ前のコミットメッセージを修正する

例として、HEADから3回分のコミットが対象範囲なので、以下のようにコマンドを実行します。

git rebase -i HEAD~3

以下のような感じで、対象となるコミット一覧と、操作の説明が表示されます。 このとき表示されるコミット一覧は一番下が一番新しいコミットです。

pick 356d95e 4行目を追加
pick e3e9b30 5行目を追加
pick fabb554 6行目を追加

# Rebase 97029ec..fabb554 onto 97029ec (3 command(s))
#
# 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
# d, drop = remove commit
#
# 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

操作の説明が表示されていますが、今回使用するのはrewordです。
コミット一覧のpickと書いてあるところをINSERTモードで、rにします。
pickになっているものは、変更を加えず使用されます。
今回は例として、二つのコミットメッセージを修正します。

r 356d95e 4行目を追加
r e3e9b30 5行目を追加
pick fabb554 6行目を追加

# Rebase 97029ec..fabb554 onto 97029ec (3 command(s))
#
# 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
# d, drop = remove commit
#
# 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

コマンドモードに戻して:wqです。

しばらく待つと自動的に以下のような表示になりますので、コミットメッセージを編集します。

4行目を追加

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date:      Fri Aug 4 19:14:05 2017 +0900
#
# interactive rebase in progress; onto 97029ec
# Last command done (1 command done):
#    r 356d95e 4行目を追加
# Next commands to do (2 remaining commands):
#    r e3e9b30 5行目を追加
#    pick 3d16339 6行目を追加
# You are currently editing a commit while rebasing branch 'hoge' on '97029ec'.
#
# Changes to be committed:
#       modified:   sample.txt
#

編集が済んだら、コマンドモードに戻って:wqです。

しばらく待つと自動的にまた同じような表示になりますので、コミットメッセージを編集します。

rにした全てのコミットメッセージを編集し終えて、以下のような感じで表示されたら成功です。

[detached HEAD bc2580e] 4行目を追加 編集メッセージ
 Date: Fri Aug 4 19:14:05 2017 +0900
 1 file changed, 1 insertion(+)
[detached HEAD 12cffc6] 5行目を追加 編集メッセージ
 Date: Fri Aug 4 19:15:19 2017 +0900
 1 file changed, 1 insertion(+)
Successfully rebased and updated refs/heads/hoge.

コミットメッセージが修正できたか確認してみる

以下のコマンドを実行してみます。

git log --oneline

以下のように編集されたのが確認できました。

721d841 6行目を追加
12cffc6 5行目を追加 編集メッセージ
bc2580e 4行目を追加 編集メッセージ
97029ec 3行目を追加
407e6f9 2行目を追加
30596dc 1行目を追加
d5d7400 Initial commit