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

后端 未结 16 1386
深忆病人
深忆病人 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 05:10

    Here is a very simple and clean way to push your changes after you have already made a commit --amend:

    git reset --soft HEAD^
    git stash
    git push -f origin master
    git stash pop
    git commit -a
    git push origin master
    

    Which does the following:

    • Reset branch head to parent commit.
    • Stash this last commit.
    • Force push to remote. The remote now doesn't have the last commit.
    • Pop your stash.
    • Commit cleanly.
    • Push to remote.

    Remember to change "origin" and "master" if applying this to a different branch or remote.

提交回复
热议问题