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
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.