2

Is there any way to automatically drop reverted commits when rebasing?

git log: (from recent to old)

4321: revert another bad commit
3210: revert bad commit
6666: another useful commit
7777: some useful commit
0123: another bad commit 
1234: bad commit

Can I get

6666: another useful commit
7777: some useful commit

without manual actions for every commit/revert pair?

Antonio Petricca
  • 8,891
  • 5
  • 36
  • 74
pacific
  • 31
  • 3
  • You do know that you can delete the lines corresponding to the commits you do not want from the text editor that opens when you run `git rebase -i`? – mkrieger1 May 28 '21 at 12:34
  • Related, if not (more general) duplicate: https://stackoverflow.com/questions/38220368/how-to-automagically-remove-commits-that-cancel-themselves-out – mkrieger1 May 28 '21 at 12:46

2 Answers2

2

You could use the auto squash feature of the git rebase command to your advantage.

This would require an additional step beforehand, however, when creating the reverting commits:

Suppose you want to revert commit 1234. Instead of doing

git revert 1234

or the equivalent from a graphical interface, you need to split this into

git revert --no-commit 1234
git commit --fixup 1234

Or you would need to edit the commit message manually to begin with "fixup!".

Now, when using git rebase -i --autosquash, or when the rebase.autoSquash option is enabled, the reverting commit will be automatically squashed with the original commit 1234. If the two cancel each other out exactly, the resulting commit will be empty.

By doing the same rebase a second time, the now empty commit will automatically be omitted.

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
0

The only solution I am facing is:

git checkout 7777 -B new-branch
git reset --hard 6666

So the log will look as you expect.

Antonio Petricca
  • 8,891
  • 5
  • 36
  • 74