Accidentally pushed commit: change git commit message

前端 未结 4 1468
抹茶落季
抹茶落季 2020-11-28 19:43

In my local repo I have one commit with an incorrect commit message.

I\'ve already published the incorrect commit message with git push.

Now the

4条回答
  •  清酒与你
    2020-11-28 19:45

    If you're not pushed the code to your remote branch(Github/Bitbucket) you can change the commit message on the command line as below.

     git commit --amend -m "Your new message"
    

    If you're working on a specific branch do this.

    git commit --amend -m "BRANCH-NAME: new message"
    

    If you've already pushed the code with wrong message then you need to be careful when changing the message. i.e after you change the commit message and try pushing it again you end up with having issues. To make it smooth follow these steps. Please read the entire answer before doing it

    git commit --amend -m "BRANCH-NAME : your new message"
    
    git push -f origin BRANCH-NAME                # Not a best practice. Read below why?
    

    Important note: When you use the force push directly you might end up with code issues that other developers are working on the same branch. So to avoid that conflicts you need to pull the code from your branch before making the force push

     git commit --amend -m "BRANCH-NAME : your new message"
     git pull origin BRANCH-NAME
     git push -f origin BRANCH-NAME
    

    This is the best practice when changing the commit message, if it was already pushed.

提交回复
热议问题