Sub topic 11
Conflicts in the middle of a stack
This chapter assumes you have run git rebase --update-refs on a stack, can read git status during a rebase, and have resolved a merge conflict before. It needs Git 2.38 or newer.
Where the rebase stops
A rebase replays commits one at a time. In a stack, the commit that conflicts is often not the top one. Suppose main changed a function that a commit in feature-2 also edits. The rebase starts at the bottom of the stack, passes feature-1 cleanly, and stops partway through feature-2.
git switch feature-3
git rebase --update-refs origin/main
# CONFLICT (content): Merge conflict in src/parser.py
# error: could not apply 9a8b7c6... Wire parser into CLIThe stack is now in a half-finished state. HEAD is detached, and the todo list still has work to do, including update-ref lines for the branches that have not been reached.
Resolving and continuing
Run git status first. It lists the commands already done, the commands still to do, and the conflicted files.
git status
# Last commands done (3 commands done):
# pick 1a2b3c4 Add parser skeleton
# update-ref refs/heads/feature-1
# pick 5d6e7f8 Add parser tests
# Next commands to do (3 remaining commands):
# pick 9a8b7c6 Wire parser into CLI
# update-ref refs/heads/feature-2
# pick 0f1e2d3 Document CLI flagFix the conflict markers in the file, then stage it and continue:
git add src/parser.py
git rebase --continueDo not run git commit yourself unless the rebase asks you to. --continue creates the commit with the original message.
Your resolution becomes part of the rewritten commit. Every later commit, including all of feature-3, is replayed on top of it. You usually resolve a conflict once, not once per branch. A later commit can still conflict if it touches the same lines, so expect to repeat the cycle.
When the rebase finishes
Git records each update-ref position as the rebase passes it and moves the branch names when the rebase completes. Until then, the branches still point at their old commits. That is what makes an abort safe:
git rebase --abortThis returns feature-1, feature-2, and feature-3 to where they were before the rebase. If a conflict looks too large to handle now, abort and try again later.
Checking the result
A clean rebase does not mean every branch still works. A resolution made in the middle of feature-2 can break the build at that branch, even if feature-3 passes. Check each branch tip:
git log --oneline --decorate --graph origin/main..feature-3
git switch feature-2
# run your tests hereIf a lower branch needs a fix, commit it there, then restack the branches above it with git rebase --update-refs from the top branch.
Reducing repeat conflicts
If you rebase a stack often, the same conflict may come back on each run. Git can remember your resolutions and reapply them:
git config --global rerere.enabled trueWith rerere (reuse recorded resolution) enabled, Git stores how you resolved a conflict. When the identical conflict appears again, it applies that resolution and leaves the files for you to review.
If a resolution goes wrong after the rebase finishes, the reflog is the recovery tool. A later chapter covers it.
Exercise
You run git rebase --update-refs origin/main on a three-branch stack. It stops with a conflict in a commit that belongs to feature-2. After you edit the file, which two commands do you run next, and what happens if you run git rebase --abort instead?
Show answer
Run git add <file> to stage the resolved file, then git rebase --continue. Aborting restores the whole stack, so feature-1, feature-2, and feature-3 all return to their pre-rebase commits.