0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【Git】commitメッセージを変更したい (git rebase -i)

Last updated at Posted at 2020-02-28

こんなとき

あぁ〜、コミットメッセージ変更したいなぁ〜。

方法

git log:n個前のコミットメッセージを変更したい!

git rebase -i HEAD~n

自動でvim起動。変えたいコミットの先頭にあるpickeditに変える。

git commit --amend -m "<新コミットメッセージ>"

git rebase --continue

git log:変更できている!

解説

git log:コミットメッセージを見る

% git log --oneline
e35aade (HEAD -> master) commit 4
754cc2f commit 2 # ここのメッセージを変えたい!
25c09e4 commit 2
58e25f0 commit 1

HEADから2個目のメッセージを変えます。

git rebase -i HEAD~n:n番目までのコミットを編集

% git rebase -i HEAD~2

HEADから2個目ということでHEAD~2
すると、vimが自動で起動します。

vim起動

    pick
     ↓
  1 edit 754cc2f commit 2
  2 pick e35aade commit 4
  3
  4 # Rebase 25c09e4..e35aade onto 25c09e4 (2 commands)
  5 #
  6 # Commands:
  7 # p, pick <commit> = use commit
  8 # r, reword <commit> = use commit, but edit the commit message
  9 # e, edit <commit> = use commit, but stop for amending
 10 # s, squash <commit> = use commit, but meld into previous commit
 11 # f, fixup <commit> = like "squash", but discard this commit's log message
 12 # x, exec <command> = run command (the rest of the line) using shell
 13 # b, break = stop here (continue rebase later with 'git rebase --continue')
 14 # d, drop <commit> = remove commit
 15 # l, label <label> = label current HEAD with a name
 16 # t, reset <label> = reset HEAD to a label
 17 # m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]
 18 # .       create a merge commit using the original merge commit's
 19 # .       message (or the oneline, if no original merge commit was
 20 # .       specified). Use -c <commit> to reword the commit message.
 21 #
 22 # These lines can be re-ordered; they are executed from top to bottom.
 23 #
 24 # If you remove a line here THAT COMMIT WILL BE LOST.
 25 #
 26 # However, if you remove everything, the rebase will be aborted.
 27 #
 28 # Note that empty commits are commented out

1行目のpickeditに変えて終了。
zshに戻ると、

Stopped at 754cc2f...  commit 2
You can amend the commit now, with

  git commit --amend

Once you are satisfied with your changes, run

  git rebase --continue

と出ています。

git commit --amend -m "<新コミットメッセージ>":コミットメッセージ変更

% git commit --amend -m "commit 3"
[detached HEAD eaf967d] commit 3
 Date: Xxx Xxx 00 00:00:00 0000 +0000
 1 file changed, 2 deletions(-)
 delete mode 100644 test2.html

ここで、変更したいメッセージを入力します。

git rebase --continue:rebaseの終了

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

git log:コミットメッセージを見る

% git  log --oneline
0b421f9 (HEAD -> master) commit 4
eaf967d commit 3 # 変わっている
25c09e4 commit 2
58e25f0 commit 1

無事変更されました。

余談

git rebase について

更新中...

0
1
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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?