0

In git, is it possible to revert series of commits with a single commit? I have the following history, from which I want to revert commits B and C

E (HEAD)
|
D
|
C
|
B
|
A

Can this be done with the reset command, as it's possible to revert all the following commits by hard resetting to target commit, and soft resetting back to head?

5
  • I think you are looking for rebase, not reset Commented Sep 11, 2017 at 4:33
  • No, I don't want to rewrite the history. I want to create a new commit, which undoes the changes introduced by the series of commits Commented Sep 11, 2017 at 4:38
  • That's called rewriting history. Take a look at interactive rebase Commented Sep 11, 2017 at 5:06
  • You're trying to squash some , discard others. That's what interactive rebase is for. Sorry , mobile Commented Sep 11, 2017 at 5:06
  • For me, rewriting history is losing some commits (or rewriting them). I don't considering adding a new commit reverting some changes as rewriting the history. Commented Sep 11, 2017 at 6:19

2 Answers 2

4

You could revert multiple commits in separate commits and then squash them into one?

The real question here is why do you want to revert everything in a single commit - It would be much clearer to do them in separate commits in a single separate branch.

2

Yes, use git revert -n (on each commit to be backed-out) followed by git commit. You'll get one big commit that has the effect of undoing multiple commits. It's rarely a great idea, though: it is difficult to tell what got reverted and why. (On the other hand, reverting many small commits is also rarely a great idea, so in some cases you must choose which method is least-bad.)

Not the answer you're looking for? Browse other questions tagged or ask your own question.