git 修改 commit message

作者:周星 发布:2015-11-12

    当我们不小心提交错了 commit message,其实是有办法修改的,今天给大家分享如何修改历史的 commit message.如果只是想修改上一次的 commit message,那就非常好办了,假如我已经执行

git add .
git commit -m 'this is a wrong commit message'


git commit --amend


this is a wrong commit message

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# On branch master
# Your branch is ahead of 'origin/master' by 1 commit.
#   (use "git push" to publish your local commits)
# Changes to be committed:
#,new file:   test.rb

然后修改 message 为正确的 message,保存退出即可。

但是如果我们想修改历史中某一次的 commit message,那就不能用 amend 了,我们可以使用用 rebase

git rebase -i HEAD~5


pick a5bddf8 Add gui support
pick a19504a remove blank line and test git
pick b0ea110 Add autocomplete to vim plugins. dancer
pick a9f600a remove blank line
pick 456a72a Add autocomplete to vim plugins. dancer
pick 3905436 ss

# Rebase 8345393..3905436 onto 8345393
# 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
# 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

下面的注释清晰的告诉我们 pick/reword/edit 等命令的意义了,想修改哪一个 message,就把前面的 pick 改成 edit,然后保存退出,你会看到这样一段文字:

Stopped at a9f600a03f5935d126db61ddd9d3d0280ef7f924... remove blank lineYou can amend the commit now, with

git commit --amend

Once you are satisfied with your changes, run

git rebase --continue

很明显,接下来执行 git commit --amend 修改 message, 之后再 git rebase --continue 即可