Git Rebase拒绝压缩合并提交的原因及解决方法
Git refuses to squash merge commits because they have two parent commits (tracking the branches you merged). Squashing a merge would discard that branch history, and the -p (--preserve-merges) flag you used is explicitly meant to keep your merge structure intact—so trying to squash a merge directly conflicts with that goal.
You have a few options depending on whether you want to keep the merge history or flatten all commits:
Option 1: Keep Merge Commits & Adjust Squashing
If preserving the merge with develop is important, modify your interactive rebase todo list to keep the merge commit as a pick instead of trying to squash it. You can still squash the regular commits around it:
- If your rebase is already stuck, abort it first:
git rebase --abort - Restart the interactive rebase with
-p:git rebase -p -i HEAD~14 - In the todo list, change the line for merge commit
656d21e1bfafromstop. Your adjusted list will look like this:pick 290c32355fd Squash all commits s 4aa246bc4c5 Squash Commits pick 656d21e1bfa merge with develop s a7448275954 tasks to do s 4c6198cd0f0 fix print pick f101fff106d Merge remote-tracking branch 'origin' into into TEST_ENGINE s 30f9adbe65a remove unused check s 7343e3565ad fix error s 1e3dc014f67 fix isokToupdate s c16412a7f83 fix isokToupdate pick 6a1cb904b73 Merge remote-tracking branch 'origin' into TEST_ENGINE pick 89001a588c3 Merge remote-tracking branch 'origin' into TEST_ENGINE s a32ce67d389 fix namespace s 2d0c8ee55e8 Refactor code and correct functional errors - Save and exit the editor—Git will now process the rebase without blocking, preserving your merge commits while squashing the regular commits into the nearest preceding pick.
Option 2: Flatten All Commits (Discard Merge History)
If you don’t need to track the merge branches and just want to combine all 14 commits into a single one, drop the -p flag entirely:
- Abort the current rebase:
git rebase --abort - Run the rebase without preserving merges:
git rebase -i HEAD~14 - Set the first commit to
pickand change all other lines (including merge commits) tos(squash). This tells Git to replay every commit as a patch and combine them all into one. Note: You might hit more conflicts here since Git is flattening the merge structure, but once resolved, you’ll end up with a single commit containing all changes.
Option 3: Adjust Individual Merge Commits
If you just need to tweak a merge commit’s message (not squash it), replace s with r (reword) in the todo list. If you need to modify the merge’s content, use e (edit)—but this is more advanced and requires manually resolving the merge again.
内容的提问来源于stack exchange,提问作者gringo

