Search code examples
gitgit-mergerebasegit-commitgit-rewrite-history

Automatic rebase to drop a commit that modifies a file?


I'm new to git rebase and doing something a bit wacky and stupid. So let's say I have these commits which all modify the same file file.txt

a -> b -> c -> d -> e

I want to just remove commit c.

My strategy for this is to rebase b onto d, keeping d's changes so it reads like this

a -> b -> d -> e

I'm trying to do this with this command

git rebase --rebase-merges --onto c^ c

This results in a failed merge, I've also tried it with -s ours but that fails as well.

This works when each commit modifies a different file, however.

What should I do to satisfy this goal?


Solution

  • In this case, you're almost certainly getting a conflict because there's really a conflict, and there isn't a way to avoid dealing with this. You'll have to resolve the conflict and then run git rebase --continue.

    This works when each commit modifies a different file because commits that touch different files don't usually conflict (unless there's a rename or copy).