How to revert multiple git commits?

后端 未结 13 2400
被撕碎了的回忆
被撕碎了的回忆 2020-11-22 07:36

I have a git repository that looks like this:

A <- B <- C <- D <- HEAD

I want the head of the branch to point to A, i.e. I want B

13条回答
  •  长发绾君心
    2020-11-22 08:04

    This is an expansion of one of the solutions provided in Jakub's answer

    I was faced with a situation where the commits I needed to roll back were somewhat complex, with several of the commits being merge commits, and I needed to avoid rewriting history. I was not able to use a series of git revert commands because I eventually ran into conflicts between the reversion changes being added. I ended up using the following steps.

    First, check out the contents of the target commit while leaving HEAD at the tip of the branch:

    $ git checkout -f  -- .
    

    (The -- makes sure is interpreted as a commit rather than a file; the . refers to the current directory.)

    Then, determine what files were added in the commits being rolled back, and thus need to be deleted:

    $ git diff --name-status --cached 
    

    Files that were added should show up with an "A" at the beginning of the line, and there should be no other differences. Now, if any files need to be removed, stage these files for removal:

    $ git rm [  ...]
    

    Finally, commit the reversion:

    $ git commit -m 'revert to '
    

    If desired, make sure that we're back to the desired state:

    $git diff  
    

    There should be no differences.

提交回复
热议问题