How to revert multiple git commits?

后端 未结 13 2373
被撕碎了的回忆
被撕碎了的回忆 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 <target-commit> -- .
    

    (The -- makes sure <target-commit> 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 <target-commit>
    

    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 <filespec>[ <filespec> ...]
    

    Finally, commit the reversion:

    $ git commit -m 'revert to <target-commit>'
    

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

    $git diff <target-commit> <current-commit>
    

    There should be no differences.

    0 讨论(0)
  • 2020-11-22 08:04

    In my opinion a very easy and clean way could be:

    go back to A

    git checkout -f A
    

    point master's head to the current state

    git symbolic-ref HEAD refs/heads/master
    

    save

    git commit
    
    0 讨论(0)
  • 2020-11-22 08:06

    Expanding what I wrote in a comment

    The general rule is that you should not rewrite (change) history that you have published, because somebody might have based their work on it. If you rewrite (change) history, you would make problems with merging their changes and with updating for them.

    So the solution is to create a new commit which reverts changes that you want to get rid of. You can do this using git revert command.

    You have the following situation:

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

    (arrows here refers to the direction of the pointer: the "parent" reference in the case of commits, the top commit in the case of branch head (branch ref), and the name of branch in the case of HEAD reference).

    What you need to create is the following:

    A <-- B  <-- C <-- D <-- [(BCD)-1]                   <-- master <-- HEAD
    

    where [(BCD)^-1] means the commit that reverts changes in commits B, C, D. Mathematics tells us that (BCD)-1 = D-1 C-1 B-1, so you can get the required situation using the following commands:

    $ git revert --no-commit D
    $ git revert --no-commit C
    $ git revert --no-commit B
    $ git commit -m "the commit message for all of them"
    

    Works for everything except merge commits.


    Alternate solution would be to checkout contents of commit A, and commit this state. Also works with merge commits. Added files will not be deleted, however. If you have any local changes git stash them first:

    $ git checkout -f A -- . # checkout that revision over the top of local files
    $ git commit -a
    

    Then you would have the following situation:

    A <-- B  <-- C <-- D <-- A'                       <-- master <-- HEAD
    

    The commit A' has the same contents as commit A, but is a different commit (commit message, parents, commit date).


    Alternate solution by Jeff Ferland, modified by Charles Bailey builds upon the same idea, but uses git reset. Here it is slightly modified, this way WORKS FOR EVERYTHING:

    $ git reset --hard A
    $ git reset --soft D # (or ORIG_HEAD or @{1} [previous location of HEAD]), all of which are D
    $ git commit
    
    0 讨论(0)
  • 2020-11-22 08:06

    Clean way which I found useful

    git revert --no-commit HEAD~3..
    

    This command reverts last 3 commits with only one commit.

    Also doesn't rewrite history.

    The .. helps create a range. Meaning HEAD~3.. is the same as HEAD~3..HEAD

    0 讨论(0)
  • 2020-11-22 08:07

    I'm so frustrated that this question can't just be answered. Every other question is in relation to how to revert correctly and preserve history. This question says "I want the head of the branch to point to A, i.e. I want B, C, D, and HEAD to disappear and I want head to be synonymous with A."

    git checkout <branch_name>
    git reset --hard <commit Hash for A>
    git push -f
    

    I learned a lot reading Jakub's post, but some guy in the company (with access to push to our "testing" branch without Pull-Request) pushed like 5 bad commits trying to fix and fix and fix a mistake he made 5 commits ago. Not only that, but one or two Pull Requests were accepted, which were now bad. So forget it, I found the last good commit (abc1234) and just ran the basic script:

    git checkout testing
    git reset --hard abc1234
    git push -f
    

    I told the other 5 guys working in this repo that they better make note of their changes for the last few hours and Wipe/Re-Branch from the latest testing. End of the story.

    0 讨论(0)
  • 2020-11-22 08:07

    None of those worked for me, so I had three commits to revert (the last three commits), so I did:

    git revert HEAD
    git revert HEAD~2
    git revert HEAD~4
    git rebase -i HEAD~3 # pick, squash, squash
    

    Worked like a charm :)

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