[Git] git rebase で過去のコミットメッセージを修正する方法

git rebase -i し、reword する。

[markdown]
`git rebase` で過去のコミットメッセージを書き換える。
必要に応じて事前に `git stash` しておく。
`-i` は `–interactive` オプション。
`HEAD~2` は HEAD から n 個のコミットを対象とする。
`HEAD~2` ではなく `` (SHA-1 チェックサム) を指定した場合、`` 以降のコミットが対象となる。

“`prettyprinted
% git rebase -i HEAD~2
“`

以下のような表示内容でエディタが立ち上がるので、修正したいコミットの pick を edit に変更する
__この場合は `reword` で良かったので、この記事を書いた。__

> [初心者でもわかる!リベースの使い方を解説します | Git編:一歩踏み出すフロントエンド入門](https://liginc.co.jp/web/tool/79390)

“`prettyprinted
1 pick 1502739 Add: items controller specs
2 pick 301fe52 Add: delivery_charges controller specs
3
4 # Rebase 518b1a9..301fe52 onto 518b1a9 (2 commands)
5 #
6 # Commands:
7 # p, pick = use commit
8 # r, reword = use commit, but edit the commit message
9 # e, edit = use commit, but stop for amending
10 # s, squash = use commit, but meld into previous commit
11 # f, fixup = like “squash”, but discard this commit’s log message
12 # x, exec = run command (the rest of the line) using shell
13 # d, drop = remove commit
14 #
15 # These lines can be re-ordered; they are executed from top to bottom.
16 #
17 # If you remove a line here THAT COMMIT WILL BE LOST.
18 #
19 # However, if you remove everything, the rebase will be aborted.
20 #
21 # Note that empty commits are commented out
“`

保存と終了すると、下記のとおり指示をされる。

“`prettyprinted
% git rebase -i HEAD~2
Stopped at 69b031f… Add: items controller specs
You can amend the commit now, with
git commit –amend
Once you are satisfied with your changes, run
git rebase –continue
“`

`git amend` で表示されるメッセージに修正を行う。

“`prettyprinted
% git amend
[detached HEAD 1502739] Add: items controller specs
Date: Fri Apr 21 17:25:33 2017 +0900
3 files changed, 69 insertions(+), 57 deletions(-)
“`

`git rebase –continue` で修正を反映させる。

“`prettyprinted
% git rebase –continue
Successfully rebased and updated refs/heads/feature/Tutorial.
“`

以上で rebase が完了する。
[/markdown]