LoginSignup
8
8

More than 5 years have passed since last update.

🍺Gitでcommitメッセージを間違えた時の変更方法

Posted at

commitメッセージを後から変更したいとき

微妙なメッセージをつけてしまうときがたまにある。

一つ前のcommitのメッセージを変更

$ git commit -m "miss message"
$ git commit --amend -m "correct message"

簡単!

二つ以上前のcommitメッセージを変更

commitメッセージをミスった時はだいたいすぐ気づくので二つ以上前のcommitメッセージを変更することは少ないと思う。

$ git add -A
$ git commit -m "Update view"

$ git add -A
$ git commit -m "Update controller"

$ git add-A
$ git commit -m "Update test"

$ git add -A 
$ git commit -m "Update Model"

$ git log --oneline          

b71d547 (HEAD -> master) Update Model
b834757 Update test
11767d4 Update controller
2d5471e Update view

二番目にcommitした時のメッセージを変更したい

"Update controller"から"Update users_controller"に変えたい

$ git rebase -i HEAD~3

3つ前のcommitなので~3をつける

こうすると

pick fc58c59 Update users_controller
pick eab1c1d Update test
pick 3d766c0 Update Model

# Rebase 2d5471e..3d766c0 onto 2d5471e (3 commands)
#
# 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

変更したいcommitのところをpickからeditに変更する。(一行目)

edit fc58c59 Update users_controller
pick eab1c1d Update test
pick 3d766c0 Update Model

# Rebase 2d5471e..3d766c0 onto 2d5471e (3 commands)
#
# 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

保存してエディタを閉じる。

一つ前のcommitメッセージを変更するのと同様に以下のようにする。

$ git commit --amend -m "Update users_controller" 

いい感じに設定を元にもどしてくれる。

$ git rebase --continue
$ git log --oneline  
3d766c0 (HEAD -> master) Update Model
eab1c1d Update test
fc58c59 Update users_controller
2d5471e Update view

ちゃんと変更されている!

8
8
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
8
8