How do I push amended commit to the remote Git repository?

后端 未结 16 1809
深忆病人
深忆病人 2020-11-22 04:42

When I\'ve worked a bit with my source code, I did my usual thing commit and then I pushed to a remote repository. But then I noticed I forgot to organize my imports in the

16条回答
  •  生来不讨喜
    2020-11-22 04:46

    If you have 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 a 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 the following 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 those 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.

提交回复
热议问题