How do I resolve merge conflicts when rebasing a branch that already contains merge commits? #190710
-
🏷️ Discussion TypeQuestion 💬 Feature/Topic AreaIssues BodyWe're using a rebase-based workflow (no merge commits), but a developer accidentally merged $ git rebase main
# Resolve conflicts in file.js
$ git add file.js
$ git rebase --continue
# Same conflicts in file.js again in the next commitI could force-push a clean history, but I want to understand the proper way to handle this. How do you cleanly handle merge commits in a rebase workflow? Guidelines |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
|
cool |
Beta Was this translation helpful? Give feedback.
-
|
This happens because Solution 1: Remove the merge commit (cleanest) # Option A: Squash the merge commit away
git rebase -i main
# Mark the merge commit as 'squash' or 'd' (delete) in the interactive editor
# Git will replay the other commits cleanly
# Option B: Cherry-pick individual commits
git rebase --abort # Stop the current rebase
git reset --hard HEAD # Go back to original state
git log --oneline # Find the commits you want
git checkout -b clean-feature
git cherry-pick <commit1> <commit2> <commit3>
# Now rebase this new branchSolution 2: Preserve the merge with git rebase --rebase=merges main
# This tells Git to recreate merge commits instead of flattening them
# Conflicts still happen once per commit, but merges are preservedSolution 3: Convert merge to rebase retroactively git rebase --onto main <merge-commit-parent> HEAD
# Rebase everything after the merge onto mainBest practice: In a rebase workflow, prefer |
Beta Was this translation helpful? Give feedback.
This happens because
git rebasereplays each commit linearly, including the merge commit (which is just another commit with two parents). When Git replays the merge commit, it has to re-merge the branches, which causes the conflict again.Solution 1: Remove the merge commit (cleanest)