Edit an incorrect commit message in command line Git

This question already has an answer here:

  • How to modify existing, unpushed commits? 27 answers

  • If it is the most recent commit, you can simply do this:

    git commit --amend
    

    This brings up the editor with the last commit message and lets you edit the message. (You can use -m if you want to wipe out the old message and use a new one.)

    And then when you push, do this:

    git push --force <repository> <branch>
    

    Be careful when using push --force. If anyone else has pushed changes to the same branch, those changes will be destroyed.

    Anyone who already pulled will not get an error message, and they will need to update (assuming they aren't making any changes themselves) by doing something like this:

    git fetch origin
    git reset --hard origin/master # Loses local commits
    

    To change a commit message of the most recent (unpushed) commit, you can simply use

    git commit --amend -m 'new message'
    

    To change messages of (unpushed) commits further in the past:

    git rebase -i [COMMIT BEFORE THE FIRST YOU WANT TO EDIT]
    

    If it is the last patch you commited from your repo, it will be on the top of your git log .

    In that case, just run the below command and push the same once again.

    git commit --amend

    Than, modify your message and push the same. Since you are not modifing any change in file, it should not give any error.

    If some patches have already come on the top of yours. Then you have to check merge dependencies also. In this case,

    either git reset --hard your commit

  • run git commit --amend

  • Push it back

  • or

  • git commit --amend -C commit-id
  • push it back
  • But you need to consider merge dependencies also.

    **

    And more best approach will be:

    **

    You can use git rebase, for example, if you want to modify back to commit xyz, run

    $ git rebase --interactive xyz^ In the default editor, modify 'pick' to 'edit' in the line whose commit you want to modify. Make your changes and then commit them with the same message you had before:

    $ git commit -a --amend --no-edit to modify the commit, and after that

    $ git rebase --continue to return back to the previous head commit.

    链接地址: http://www.djcxy.com/p/1406.html

    上一篇: 在git中更改提交消息

    下一篇: 在命令行Git中编辑不正确的提交消息