开发者

How do I edit the commit message of any commit in git? [duplicate]

开发者 https://www.devze.com 2023-01-20 05:55 出处:网络
This question already has answers here: How do I modify a specific commit? (19 answers) Closed 8 years ago.
This question already has answers here: How do I modify a specific commit? (19 answers) Closed 8 years ago.

L开发者_如何学Goet's say I have 3 unpushed commits. Now I want to change the commit message of the first or second commit (changing them for the third one is simple using git commit --amend). How to do that?


To rebound on the sub-question: is there a git commit --amend for a previous commit (and not just the last one), you could try something like (not tested yet, but Colin O'Dell mentions in the comments having written a script for it colinodell/git-amend-old):

git checkout -b tmp
git reset --hard HEAD~2
git commit -amend 
git rebase --onto tmp HEAD@{1} master

That would be like:

x---x---x---x---x
                ^
                |
               (master*) (* = current branch)

git checkout -b tmp
x---x---x---x---x
                ^
                |
               (tmp*, master) 

git reset --hard HEAD~2
x---x---x---x---x
        ^       ^
        |       |
      (tmp*) (master) 

git commit -amend 
      y (tmp*) 
     /
x---x---x---x---x
        |       ^
   (HEAD@{1})   |
          (master) 

git rebase --onto tmp HEAD@{1} master
    (tmp)
      y---x'---x' (master*) 
     /
x---x---x---x---x (only referenced in reflog)


This is a job for the powerful git rebase -i command. Also, see the Interactive Rebasing section of the Git book.

0

精彩评论

暂无评论...
验证码 换一张
取 消