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
The easy way to revert a group of commits on shared repository (that people use and you want to preserve the history) is to use git revert
in conjunction with git rev-list
. The latter one will provide you with a list of commits, the former will do the revert itself.
There are two ways to do that. If you want the revert multiple commits in a single commit use:
for i in `git rev-list <first-commit-sha>^..<last-commit-sha>`; do git revert --no-commit $i; done
this will revert a group of commits you need, but leave all the changes on your working tree, you should commit them all as usual afterward.
Another option is to have a single commit per reverted change:
for i in `git rev-list <first-commit-sha>^..<last-commit-sha>`; do git revert --no-edit -s $i; done
For instance, if you have a commit tree like
o---o---o---o---o---o--->
fff eee ffffd ccc bbb aaa
to revert the changes from eee to bbb, run
for i in `git rev-list eee^..bbb`; do git revert --no-edit -s $i; done