Is there any way to automatically rebase a large number of merge commits? The answers to another question, Rebasing a Git merge commit suggest using --preserve-merges
, but that will also rebase the non-merge commits.
This behavior is not desired when merging feature branches into master or an integration branch, and then discovering that someone else has just pushed changes. Additionally, it is useful to be able to rebase a development branch onto a new master commit, or to remove fixes from development that are not going to be used.
For instance, given the following situation:
* 7909b1a (origin/master) Merge bug/456
|\
| * f9d43b6 (origin/bug/456) 456 - Change color
|/
| * 32666f3 (HEAD, master) Merge branch 'bug/123'
| |\
|/ /
| * 0939652 (origin/bug/123, bug/123) 123 - Fix Spelling
|/
o 96c9aa9 (tag: v1.1.1)
If I run git rebase --preserve-merges origin/master
, the the following will result:
* (HEAD, master) Merge branch 'bug/123'
|\
| * 8e6ccbe 123 - Fix Spelling
|/
* 7909b1a (origin/master) Merge bug/456
|\
| * f9d43b6 (origin/bug/456) 456 - Change color
|/
| * 32666f3 (ORIG_HEAD) Merge branch 'bug/123'
| |\
|/ /
| * 0939652 (origin/bug/123, bug/123) 123 - Fix Spelling
|/
o 96c9aa9 (tag: v1.1.1)
This is not desirable, because commit 0939652 is being redone, but it is already pushed to public repos, so it should not need to be replayed. The result I want is the following:
* 710c5d7 (HEAD, master) Merge branch 'bug/123'
|\
| * 0939652 (origin/bug/123, bug/123) 123 - Fix Spelling
* | 7909b1a (origin/master) Merge bug/456
|\ \
| |/
|/|
| * f9d43b6 (origin/bug/456) 456 - Change color
|/
o 96c9aa9 (tag: v1.1.1)
I can manually reset --hard
to origin/master, and then re-merge the branches that I already did (using the --rerere-autoupdate option, so that I do not have to reresolve conflicts), but that can get quite complicated when there are 15 merges instead of 1.
Is there any built-in Git solution, or a script that can accomplish what I want?
BTW, doing git rebase -ip master
, and then not picking the non-merge commits does not work. I get
error: Commit 00... is a merge but no -m option was given.
fatal: cherry-pick failed
Could not pick 00...