Sub topic 13
Conflicts during rebase
This chapter assumes you can run git merge, read conflict markers, and finish a merge by editing a file, running git add, and committing. It covers how a conflict during git rebase differs from a merge conflict.
Why rebase conflicts feel different
A merge combines two branch tips in one step. If it conflicts, you resolve everything once and make one merge commit.
A rebase replays your commits one at a time on top of another branch. Each replay is a small merge of its own, so any of them can conflict. A branch with five commits can stop up to five times, and you resolve each stop separately.
Rebase also rewrites history. The replayed commits are new commits with new IDs. The originals remain reachable until the reflog entries expire.
A worked example
This script builds a repository where feature has two commits. The first conflicts with main. The second touches a different file.
git init -b main rebase-demo && cd rebase-demo
printf 'timeout = 10\n' > config.txt
git add config.txt && git commit -m "Add config"
git switch -c feature
printf 'timeout = 60\n' > config.txt
git commit -am "Raise timeout to 60"
printf 'retries = 3\n' > retries.txt
git add retries.txt && git commit -m "Add retries"
git switch main
printf 'timeout = 30\n' > config.txt
git commit -am "Raise timeout to 30"
git switch feature
git rebase mainGit replays "Raise timeout to 60" onto main and stops:
CONFLICT (content): Merge conflict in config.txt
error: could not apply 1a2b3c4... Raise timeout to 60
hint: Resolve all conflicts manually, mark them as resolved with
hint: "git add/rm <conflicted_files>", then run "git rebase --continue".The commit ID will differ on your machine. The second commit has not been replayed yet.
Where you are during a stopped rebase
Run git status. It reports that a rebase is in progress, which commit is being applied, and which files are unmerged.
git status
git diff
git ls-files -u
git rebase --show-current-patchgit statusshows the rebase state and the unmerged paths.git diffshows the conflicting hunks.git ls-files -ulists the base, ours, and theirs versions as stages 1, 2, and 3.git rebase --show-current-patchshows the full commit Git is trying to apply. This helps when you have forgotten what the commit was meant to do.
HEAD is detached while the rebase runs. The feature branch name is not moved until the rebase finishes. Do not worry about the detached state. It is normal here.
Ours and theirs swap meaning
Rebase builds the new history on top of the target branch. At each stop, the checked-out side is the target branch plus the commits already replayed. The commit being replayed is the incoming change.
| Operation | "ours" (`HEAD` side) | "theirs" (other side) |
|---|---|---|
| `git merge feature` on `main` | `main`, the branch you are on | `feature`, the branch you merge in |
| `git rebase main` on `feature` | `main` plus commits already replayed | your commit being replayed |
The names look reversed because they describe the mechanics, not who wrote the work. Your own work is "theirs" during a rebase.
The markers follow the same rule:
<<<<<<< HEAD
timeout = 30
=======
timeout = 60
>>>>>>> 1a2b3c4 (Raise timeout to 60)The top half, labelled HEAD, is the version already on main. The bottom half is your commit, labelled with its short ID and message.
Resolving and continuing
Resolving works as in a merge. Edit the file to the correct combined content with no marker lines left. Suppose you decide the right value is 45:
printf 'timeout = 45\n' > config.txt
git add config.txt
git rebase --continuegit rebase --continue opens your editor with the original commit message. Save and close to accept it. In scripts, GIT_EDITOR=true git rebase --continue accepts the message without opening an editor.
Git then replays "Add retries". It touches a different file, so it applies cleanly and the rebase finishes. Check the result:
git log --oneline --graphYou should see a straight line: "Add retries", "Raise timeout to 60", "Raise timeout to 30", "Add config". The commit "Raise timeout to 60" now contains your resolved change, so its diff against its new parent sets the value to 45.
Stage files with git add, not git commit. Running git commit mid-rebase creates an extra commit that was never part of the plan. git rebase --continue makes the commit for you.
Three ways out
At a stop you have three commands besides --continue:
git rebase --abortreturns the branch to exactly how it was before you started. Use it when the conflicts are bigger than expected or you picked the wrong target.git rebase --skipdrops the commit being replayed. Use it only when the target branch already contains an equivalent change.git rebase --continueaccepts your resolution and moves to the next commit.
If you stage a resolution that makes the commit identical to its parent, Git reports that there is nothing to commit. --continue then refuses to proceed. The commit has become redundant, so run git rebase --skip.
Taking one side
For a single file, use the same commands as in a merge, with the swapped meaning in mind:
# keep the target branch's version of the file
git checkout --ours -- config.txt
git add config.txt
# keep the version from the commit being replayed
git checkout --theirs -- config.txt
git add config.txtYou may see git rebase -X ours or -X theirs suggested to resolve all conflicts automatically. These options pick a side only for conflicting hunks. Merged text can still be wrong, and the "ours" side is the target branch. Prefer resolving by hand unless you have a clear reason.
Repeated conflicts and rerere
A long branch can hit the same conflict repeatedly, because several commits edit the same lines. Enable rerere so Git records your resolution and reapplies it when it sees the same conflict again:
git config --global rerere.enabled trueGit still stops, but the files are already resolved. Review them, run git add, and continue. rerere also helps if you abort and restart a rebase, or rebase the same branch again later.
If a branch produces many conflicts, consider squashing its commits first with git rebase -i, so you resolve fewer overlapping replays. A merge may also suit the situation better, since it resolves everything once.
Recovering and publishing
If a resolution goes wrong after the rebase has finished, the old commits are still in the reflog. ORIG_HEAD points at the branch tip from before the rebase:
git reset --hard ORIG_HEADThis discards uncommitted work, so check git status first. Another command may have updated ORIG_HEAD since the rebase, so look at git reflog if unsure.
Because rebase rewrites commits, a branch you already pushed now differs from the remote copy. Updating it needs a force push:
git push --force-with-lease--force-with-lease refuses to overwrite remote commits you have not seen. Plain --force does not. Do not rebase branches that other people have based work on.
Exercise
You run git rebase main on feature. Git stops with a conflict in app.py. You want the version of the file from your own commit being replayed. Which checkout option do you use, what do you run afterward, and what if the stop turns out to be a mistake?
Show answer
Use git checkout --theirs -- app.py, because during a rebase "theirs" is the commit being replayed. Then run git add app.py and git rebase --continue. If you decide the rebase was a mistake, run git rebase --abort to restore the branch to its state before the rebase.
Exercise
After you resolve a conflict and run git add, git rebase --continue reports that there is nothing to commit. What happened, and what is the right next step?
Show answer
Your resolution made the replayed commit identical to its new parent, so the commit has no effect. Run git rebase --skip to drop it and move on.