How can I delete a commit to git

This question already has an answer here:

  • Delete commits from a branch in Git 23 answers

  • I am going to lay out all the different ways to delete commits for you and when you should use them. Before you do any of these I highly recommend you copy your branch to another branch just to be safe.

    Do this by doing git checkout -b copy_branch and then switch back to your original branch by doing git checkout the_branch_i_want_to_delete_from_again

    If you have already pushed, as in your case, you can skip to #3, but if you haven't pushed you can look at 1 and 2.

    1) I have not pushed yet or am working alone and the commit(s) I want to remove are the most recent commits:

    If i have: A---B---C---D

    and I want to delete C and D.

    Then do git reset --hard sha_of_B

    which results in: A---B

    If you have already pushed, as in your case, you could still do it this way then do a git push --force origin the_branch , but this is not recommended as you could mess other people up working on this project. You should follow #3 instead.

    2) I have not pushed yet or am working alone and the commits(s) I want to remove are in the middle of my branch:

    If i have: A---B---C---D

    and I want do delete C.

    git rebase -i sha_of_B_the_commit_before_the_one_i_want_to_delete
    

    which opens up the extremely useful interactive rebase screen:

    pick sha_of_C C
    pick sha_of_D D
    

    As git prompts you "# If you remove a line here THAT COMMIT WILL BE LOST." that is what we are going to do.

    I delete the line pick sha_of_C C , which leaves me with:

    pick sha_of_D D
    

    I save it in vi with :wq, which results in:

    A---B---D

    If you have already pushed, as in your case, you could still do it this way then do a git push --force origin the_branch , but this is not recommended as you could mess other people up working on this project. You should follow #3 instead.

    3) I have already pushed and I am working with other people:

    If I have: A---B---C---D and I want to delete C and D.

    Then do

    git revert sha_of_D
    

    Note that you may have to resolve conflicts here and then commit. Then do

    git revert sha_of_C
    

    Note that you may have to resolve conflicts here and then commit.

    This results in:

    A---B---C---D---Reverted_D---Reverted_C

    This is safe to push as you are really just adding a commit that reverses all of C's and D's changes, essentially deleting it.

    You should generally avoid doing a git push --force at all costs unless absolutely necessary. But If you are about to do a push force, use protection, you should definitely make a copy of your branch before doing so. It is generally a good rule to just stick to #3 if you have already pushed at all.

    Hopefully this is helpful.

    Dan


    To delete last commit

    $ git reset --hard HEAD^
    

    To delete the latest 2 commits

    $ git reset --hard HEAD~2
    

    You can download the branch locally. Then delete the commits and perform a push --force

    $ git pull origin
    $ git checkout origin/<branchname>
    $ git checkout -b <branchname>
    $ git reset --hard HEAD~2
    $ git push origin <branchname> --force
    
    链接地址: http://www.djcxy.com/p/8998.html

    上一篇: 从存储库历史中删除提交

    下一篇: 我怎样才能删除提交给git