Remove a git commit which has not been pushed

前端 未结 11 683
抹茶落季
抹茶落季 2020-12-04 04:34

I did a git commit but I have not pushed it to the repository yet. So when I do git status, I get \'# Your branch is ahead of \'master\' by 1 commi

相关标签:
11条回答
  • 2020-12-04 05:02

    IF you have NOT pushed your changes to remote

    git reset HEAD~1
    

    Check if the working copy is clean by git status.

    ELSE you have pushed your changes to remote

    git revert HEAD
    

    This command will revert/remove the last one commit/change and then you can push

    0 讨论(0)
  • 2020-12-04 05:02

    Remove the last commit before push

    git reset --soft HEAD~1

    1 means the last commit, if you want to remove two last use 2, and so forth*

    0 讨论(0)
  • 2020-12-04 05:06
    git reset --hard origin/master
    

    to reset it to whatever the origin was at.

    This was posted by @bdonlan in the comments. I added this answer for people who don't read comments.

    0 讨论(0)
  • 2020-12-04 05:10

    Simply type in the console :

    $ git reset HEAD~
    

    This command discards all local commits ahead of the remote HEAD

    0 讨论(0)
  • 2020-12-04 05:10

    This is what I do:

    First checkout your branch (for my case master branch):

    git checkout master
    

    Then reset to remote HEAD^ (it'll remove all your local changes), force clean and pull:

    git reset HEAD^ --hard && git clean -df && git pull
    
    0 讨论(0)
提交回复
热议问题