LoginSignup
19
18

More than 5 years have passed since last update.

Gitのコミットメッセージを後から修正する

Posted at

概要

Gitのコミットメッセージ間違えてしまった場合の修正方法をまとめました。

環境

  • macOS High Sierra
  • ターミナル

直前のコミットメッセージを修正する

以下の例のように、直前のコミットメッセージを間違えてしまった場合。

直前のコミットでissueとの紐付けを忘れている
$ git log --oneline
503fd63 test: テストコードの修正
...

解決策

$ git commit --amendコマンドを利用します。

手順

  1. ターミナルで$ git commit --amendを実行する。
  2. viエディタが起動する。
  3. i で編集モードに切り替える。
  4. コメントを修正する。
  5. Escキーで編集モードを終了し、wqで保存する。
$ git commit --amend
hint: Waiting for your editor to close the file... 
fix: xxxxのバグを修正

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date:      Sun Nov 4 16:56:53 2018 +0900
#
# On branch master
# Your branch is up to date with 'origin/master'.
#
# Changes to be committed:
#       modified:   test/widget_test.dart

修正確認

$ git log --oneline
503fd63 test: #78 テストコードの修正
...

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

以下の例のように、2つ前のコミットメッセージを間違えてしまった場合。

2つ前のコミットで`機能`と`昨日`をtypoしている
$ git log --oneline
33d7b8d fix: #91 ○○が重複登録できてしまうバグを修正
9a60887 feat: #89 ○○を変更する昨日を追加
86abff0 style: #12 インデントを修正
...

解決策

$ git rebase -iを利用します。

手順

  1. 修正対象のコミットを指定する
  2. コミットメッセージを修正する。
  3. rebaseを終了する
修正対象のコミットを指定する
  1. ターミナルで$ git rebase -i HEAD~2を実行する
    HEAD~のあとの数字は修正したいコミットまでの数字
  2. viエディタが起動する。
  3. i で編集モードに切り替える。
  4. 修正したいコミットメッセージの先頭にあるpickの文字列をeditに書き替える。
  5. Escキーで編集モードを終了し、wq で保存する。
$ git rebase -i の実行結果
pick 9a60887 feat: #89 ○○を変更する昨日を追加
pick 33d7b8d fix: #91 ○○が重複登録できてしまうバグを修正

# Rebase 86abff0..33d7b8d onto 86abff0 (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
# 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.
#
#
# Note that empty commits are commented out
コミットメッセージを修正する。
  1. $ git commit --amendを実行する。
  2. viエディタが起動する。
  3. i で編集モードに切り替える。
  4. コメントを修正する。
  5. Escキーで編集モードを終了し、wqで保存する。
rebaseを終了する

$ git rebase --continue を実行すると、rebase が終了します。
※今回はeditに変更したコミットが1つなので終了しますが、2つ以上変更している場合は次のコミット移動します

$ git rebase --continue
Successfully rebased and updated refs/heads/master.

修正確認

$ git log --oneline
33d7b8d fix: #91 ○○が重複登録できてしまうバグを修正
9a60887 feat: #89 ○○を変更する機能を追加
86abff0 style: #12 インデントを修正
19
18
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
19
18