Sub topic 11
Conflicts in the middle of a stack
This chapter assumes you have followed the earlier chapters. You know what a stack is, you can run git rebase --update-refs, and you can read update-ref lines in the todo list. It covers what happens when a rebase of a stack stops on a conflict, how to resolve it so the fix lands in the right branch, and how to check the result.
Why a stack conflicts more than a single branch
A rebase replays commits one at a time on top of the new base. In a stack, commits from several branches sit in one sequence, so a conflict can appear at any point in it.
Consider the stack from earlier chapters, with main having moved on:
feature-1adds the parser skeleton.feature-2adds tests and wires the parser into the CLI.feature-3documents the CLI flag.
Suppose someone changed the same lines of src/parser.py on main. The first commit of feature-1 is replayed first, so it conflicts first. The rest of the stack has not been touched yet.
Two things make stack conflicts different from single-branch conflicts:
- Conflicts cascade. Your resolution in
feature-1becomes the base for every later commit. A later commit that touches the same code may conflict again, because the code it expected is now different. - Each resolution belongs to a branch. The conflict is resolved in the commit being replayed, so the fix is stored in that commit, and that commit belongs to a specific branch.
What the rebase looks like when it stops
Start the rebase as usual:
git fetch origin
git switch feature-3
git rebase --update-refs origin/mainWhen a pick conflicts, Git stops and prints something like this:
Auto-merging src/parser.py
CONFLICT (content): Merge conflict in src/parser.py
error: could not apply 1a2b3c4... Add parser skeleton
hint: Resolve all conflicts manually, mark them as resolved with
hint: "git add/rm <conflicted_files>", then run "git rebase --continue".
hint: You can instead skip this commit: run "git rebase --skip".
hint: To abort and get back to the state before "git rebase", run "git rebase --abort".The git status output tells you where you are in the todo list:
git statusIt reports the rebase in progress, the commands already done (the last one is the commit that failed), and the commands still to do. The remaining commands include the update-ref lines. That is the quickest way to see which branch you are working inside: look for the next update-ref line below the current commit.
To see the commit that failed, use:
git rebase --show-current-patchWhen do the branches move?
Git does not move each branch the instant it reaches an update-ref line. It records the new commit ID for each branch as the rebase proceeds, and it updates the branch refs when the whole rebase finishes. You will then see a message listing the refs that were updated.
This has a useful consequence. While you are stopped on a conflict, feature-1 and feature-2 still point at their old commits. If you abort, nothing has changed on any branch.
Resolving a conflict
The resolution loop is the same as for any rebase.
- Open the conflicted file and find the markers.
- Edit the file to the result you want, and remove the markers.
- Stage the file with
git add. - Run
git rebase --continue.
A conflicted section in src/parser.py looks like this:
<<<<<<< HEAD
def parse(text, strict=False):
=======
def parse(text):
>>>>>>> 1a2b3c4 (Add parser skeleton)During a rebase, HEAD is the new base you are building on (here, origin/main plus anything already replayed). The section under the commit ID is your commit. In this example, main added a strict argument, and your commit wrote the skeleton without it. Keep both ideas:
def parse(text, strict=False):Then continue:
git add src/parser.py
git rebase --continueGit opens the commit message editor for the rewritten commit. Keep the message, unless the resolution changed what the commit does.
The fix lands in the commit that conflicted
Do not wait and fix the problem later in a new commit at the top of the stack. The resolution belongs in the commit that conflicted, because that commit is part of feature-1. If you fixed it at the top, feature-1 would not build on its own, and reviewers of feature-1 would see broken code.
A conflict may repeat in the next commit
After you finish feature-1, the next commit, "Wire parser into CLI", is replayed on top of your resolved code. If it calls parse(text) and your resolution changed the signature, it may conflict again. Resolve it the same way, in that commit.
Git can remember resolutions and replay them. Enable it once:
git config --global rerere.enabled trueWith rerere ("reuse recorded resolution"), Git records how you resolved a conflict. If the same conflict appears again, for example in a later rebase of the same stack, Git applies the saved resolution automatically. You still need to check the result and run git add.
Skipping, editing and aborting
Three commands handle the cases where resolution is not straightforward.
| Command | What it does | When to use it |
|---|---|---|
| `git rebase --continue` | Commits your resolution and moves on | After you staged the resolved files |
| `git rebase --skip` | Drops the current commit | The commit became empty, or its change already exists on the new base |
| `git rebase --abort` | Restores the state before the rebase | You want to start over or think again |
A commit becomes empty when your resolution makes it identical to what the base already contains. Git then says there is nothing to commit. Run git rebase --skip for that commit.
If you skip the only commit of a branch, that branch's update-ref line still runs. The branch then points at the last commit before it, which is the previous branch's tip. Check that this is what you intended.
You can also change the plan during a stop with git rebase --edit-todo. For example, you can reorder commits to avoid a repeated conflict. Do this only if you understand the dependency between the commits.
Checking that each branch still works
A stack is only useful if each branch can be reviewed and merged on its own. A conflict resolution in the middle can break a branch in the middle, even when the top builds.
Run your tests at each step with --exec:
git rebase --update-refs --exec "python -m pytest -q" origin/mainGit inserts an exec line after each commit. If the tests fail, the rebase stops there, like a conflict. Fix the code, amend or stage the fix, and run git rebase --continue. Because the failure appears right at the commit that caused it, the fix lands in the right branch.
Running the tests after every commit is slow for large stacks. A cheaper alternative is to test only at branch tips. After the rebase, loop over the branches:
for b in feature-1 feature-2 feature-3; do
git switch "$b" && python -m pytest -q || break
doneThe || break stops at the first failing branch, which is the one to fix.
Comparing the old stack and the new stack
To check that the rebase changed only what you expected, compare the old and new versions of the stack with git range-diff. After a rebase, the previous tip of the branch is in the reflog, and the previous origin/main is in the reflog of the remote-tracking branch:
git range-diff origin/main@{1}..feature-3@{1} origin/main..feature-3The output pairs each old commit with its rewritten version. Unchanged commits show as =. Commits whose content changed show as !, with the difference between the two versions. Those ! commits are where your conflict resolutions are. If a commit that you did not touch shows a change, look at it closely.
This only works while the reflog entries exist, and origin/main@{1} is only the previous value if you ran git fetch once since the last update. If you fetched several times, use a larger number, or use the commit ID you noted before.
Common mistakes
- Resolving by taking "ours" or "theirs" for the whole file. In a rebase these names are reversed from what many people expect. "Ours" is the new base, and "theirs" is the commit being replayed. Edit the file by hand unless you are sure.
- Running
git commitinstead ofgit rebase --continue. Committing manually can create an extra commit, and the todo list does not know about it. Stage the files and continue. - Aborting after many resolved conflicts.
git rebase --abortthrows away all of that work. If you only want to undo the last step, there is no per-commit undo. Plan forrererebefore a long rebase, so that a second attempt is cheap. - Forgetting to force-push every branch. All branches in the stack have new commit IDs after the rebase. Push all of them, as covered in the earlier chapter on pushing the stack.
If a resolution went wrong and the rebase has already finished, the reflog still has the old branch positions. A later chapter covers recovering from it.
Exercise
You run git rebase --update-refs origin/main on feature-3. The rebase stops on a conflict in the first commit of feature-2. Where do feature-1, feature-2 and feature-3 point at this moment, and what happens to them if you run git rebase --abort?
Show answer
All three branches still point at their old commits, because Git moves the branch refs only when the whole rebase finishes. Running git rebase --abort returns the repository to the state before the rebase, so none of the branches changes.
Exercise
A conflict appears in a commit of feature-1. You are tempted to take the rebase through with the conflict markers removed quickly, and fix the code properly in a new commit at the top of feature-3 later. Why is this a poor choice for the stack?
Show answer
The resolution belongs in the commit that conflicted. If you fix the problem later at the top, feature-1 and feature-2 contain broken or inconsistent code, so they cannot be reviewed, tested or merged on their own. Resolve the conflict in the commit where it appears, and use git rebase --continue.