Remove a git commit which has not been pushed

前端 未结 11 682
抹茶落季
抹茶落季 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 04:43

    Actually, when you use git reset, you should refer to the commit that you are resetting to; so you would want the db0c078 commit, probably.

    An easier version would be git reset --hard HEAD^, to reset to the previous commit before the current head; that way you don't have to be copying around commit IDs.

    Beware when you do any git reset --hard, as you can lose any uncommitted changes you have. You might want to check git status to make sure your working copy is clean, or that you do want to blow away any changes that are there.

    In addition, instead of HEAD you can use origin/master as reference, as suggested by @bdonlan in the comments: git reset --hard origin/master

    0 讨论(0)
  • 2020-12-04 04:45

    One way would be to delete the local branch and checkout that branch from the server if your local branch is ahead of remote by multiple commits and you need to uncommit all of them.

    0 讨论(0)
  • 2020-12-04 04:46

    I just had the same problem and ended up doing:

    git rebase -i HEAD~N
    

    (N is the number of commits git will show you)

    That prompts your text editor and then you can remove the commit you want by deleting the line associated with it.

    0 讨论(0)
  • 2020-12-04 04:47

    I have experienced the same situation I did the below as this much easier. By passing commit-Id you can reach to the particular commit you want to go:

    git reset --hard {commit-id}
    

    As you want to remove your last commit so you need to pass the commit-Id where you need to move your pointer:

    git reset --hard db0c078d5286b837532ff5e276dcf91885df2296
    
    0 讨论(0)
  • 2020-12-04 04:53

    There are two branches to this question (Rolling back a commit does not mean I want to lose all my local changes):

    1. To revert the latest commit and discard changes in the committed file do:

    git reset --hard HEAD~1

    2. To revert the latest commit but retain the local changes (on disk) do:

    git reset --soft HEAD~1

    This (the later command) will take you to the state you would have been if you did git add.

    If you want to unstage the files after that, do

    git reset

    Now you can make more changes before adding and then committing again.

    0 讨论(0)
  • 2020-12-04 04:58

    I believe that one of those will fit your need

    1 - Undo commit and keep all files staged: git reset --soft HEAD~;

    2 - Undo commit and unstage all files: git reset HEAD~;

    3 - Undo the commit and completely remove all changes: git reset --hard HEAD~;

    here is were I found the answer

    0 讨论(0)
提交回复
热议问题