Undo or remove changes from a previous Git commit

This question already has an answer here:

  • Completely remove (old) git commits from history 1 answer

  • You can always revert the changes.

    git revert C
    

    If you want to rewrite history, this is what rebase is for. You need to:

    git rebase -i A
    

    Then remove C from the list of the commits.

    Longer explanation:

    First option reverts the change by creating a new commit which is the opposite of the original commit. So the repository would look like:

    A > B > C > D > E > C`

    Where C` is opposite of C. Basically, same as patch --reverse

    Second option changes the history. rebase allows you to select specific commits, change order of commits, and much more. I suggest reading git-rebase. The -i flag is for interactive mode which let the user edit the list of commits before rebasing.

    If you remove C from the list of commits and perform the rebase, the actual output would be

    A > B > D > E

    You have no longer history of C in the branch.

    Note: In both cases you may have conflicts in the process. In first case because you are applying C` over E, and in the second case because you applying D over B.


    try git revert C

    ...and best also read man git-revert


    You can't do it and get A > B > D > E . You will always end up with A > B > D' > E'

    To remove the changes introduced in C you can use

    git rebase --onto <newbase> <oldbase> <commit/branch>
    

    in your case:

    git rebase --onto B C E
    

    This will place changes introduced in D..E from the old base C onto the new base B (if you do not hit any conflicts) resulting in

    A > B > D' > E'
    

    It is important to note that D and E get new sha IDs resulting in D' and E' so if you already published C , D or E use the revert approach ( git revert C ) or you might mess up other peoples history.

    链接地址: http://www.djcxy.com/p/49792.html

    上一篇: 从已推送的合并分支恢复特定提交

    下一篇: 撤消或删除以前的Git提交中的更改