GIT修改commit信息

有的時候Git的commit message輸入錯了,怎麼辦?怎麼回滾?有以下兩種場景:

  1. 修改最後一次的log message
  2. 修改歷史的log message

環境準備

git init
touch file_b
ga .
gcam "commit file_b"
touch file_c
ga .
gcam "commit file_xxxx"
touch file_d
ga .
gcam "commit file_d"

我使用的是zsh git插件,命令都是簡寫。

commit 071673c8bf355162817c9ff1d6232d53e7b2d029
Author: pengganyu <peng_gy@163.com>
Date:   Tue Aug 29 09:14:10 2017 +0800

    commit file_d

commit b2e67f1f65039aeb18bbf08374444184a827e567
Author: pengganyu <peng_gy@163.com>
Date:   Tue Aug 29 09:14:00 2017 +0800

    commit file_xxxx

commit 1d9e72fa7ae8287fb6ce5e237dc8be859813348b
Author: pengganyu <peng_gy@163.com>
Date:   Tue Aug 29 09:13:45 2017 +0800

    commit file_b

修改上一次log message

git commit --amend

git會彈出修改message的信息,直接修改即可

## 上一次提交message:commit file_d
commit file_d_xxxx
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date:      Tue Aug 29 09:14:10 2017 +0800
#
# On branch master
# Changes to be committed:
#       new file:   file_d
#

修改後 git log信息如下:

commit 9ab87fac1c9dfd38c0cc7dec838445912b5e45cc
Author: pengganyu <peng_gy@163.com>
Date:   Tue Aug 29 09:14:10 2017 +0800

    commit file_d_xxx

commit b2e67f1f65039aeb18bbf08374444184a827e567
Author: pengganyu <peng_gy@163.com>
Date:   Tue Aug 29 09:14:00 2017 +0800

    commit file_xxxx

commit 1d9e72fa7ae8287fb6ce5e237dc8be859813348b
Author: pengganyu <peng_gy@163.com>
Date:   Tue Aug 29 09:13:45 2017 +0800

    commit file_b

修改多次歷史記錄

git rebase -i commit_id ## 只能修改commit_id 之前的log message
git rebase -i --root  ## 修改第一次及之前的log message
git rebase -i HEAD~2   ## 修改倒數第二次及之前的log message
git rebase -i HEAD~1   ## 修改最後一次提交的log message
git rebase -i --root
pick 62252dd commit file_b
pick b443d79 commit file_xxxx
pick dcce1df commit file_d_xxx

# Rebase dcce1df onto b88e1cb (3 command(s))
#
# 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

將需要修改的某一次提交信息前的pick改爲reword,wq保存後即可彈出message的修改界面

pick 62252dd commit file_b
reword b443d79 commit file_xxxx
pick dcce1df commit file_d_xxx

wq保存後的界面

commit file_xxxx

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date:      Tue Aug 29 09:14:00 2017 +0800
#
# interactive rebase in progress; onto b88e1cb
# Last commands done (2 commands done):
#    pick 62252dd commit file_b
#    reword b443d79 commit file_xxxx
# Next command to do (1 remaining command):
#    pick dcce1df commit file_d_xxx
# You are currently editing a commit while rebasing branch 'master' on 'b88e1cb'.
#
# Changes to be committed:
#       new file:   file_c

修改message信息如下:

commit file_xxxx_reword

保存後的git log 如下

commit 9942bfeb2cf1415cfb44c83e5ddef13f0351a7b2
Author: pengganyu <peng_gy@163.com>
Date:   Tue Aug 29 09:14:10 2017 +0800

    commit file_d_xxx

commit b889d15f9d1b8ee8120c9166a7bbb1d8e84715b1
Author: pengganyu <peng_gy@163.com>
Date:   Tue Aug 29 09:14:00 2017 +0800

    commit file_xxxx_reword

commit eb0c217bfcc941c725df4411ee8d7159937c6b1b
Author: pengganyu <peng_gy@163.com>
Date:   Tue Aug 29 09:13:45 2017 +0800

    commit file_b

如果想要修改多個message,將指定的message前的pick修改爲reword即可,修改多少個,就會自動彈出多少次的修改界面

參考資料

  1. Git-Tools-Rewriting-History
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章