Sub topic 7
Handling Conflicts: continue, skip, and abort
This chapter assumes you can read a three-argument git rebase --onto <newbase> <upstream> <branch> command and know that the commits in upstream..branch are the ones replayed. It also assumes you can read a merge conflict marker. Here you will learn what Git does when a replayed commit does not apply cleanly, and the three commands that control what happens next.
Why a rebase stops
A rebase does not move commits. It copies them. For each commit in the range, Git applies that commit's changes on top of the new base, one commit at a time, in order. Each copy is a three-way merge:
- The base of the merge is the original parent of the commit being replayed.
- One side is the commit being replayed.
- The other side is the commit you have built so far on the new base.
If both sides changed the same lines, Git cannot choose. It stops, writes conflict markers into the files, and waits for you.
--onto makes conflicts more likely than a plain rebase. You are moving commits to a base that may differ a lot from the one they were written against. In the chapter's running example, topic was started from next, and you are replaying it onto main. Any file that main and next changed differently can conflict.
Commits that were already replayed stay replayed. Git does not roll back. It leaves your repository in a "rebase in progress" state until you finish or cancel.
A conflict you can reproduce
The following script builds a small repository. main and topic both change the timeout line that next first changed. Run it in an empty directory. It needs Git 2.28 or later for git init -b, and a configured user.name and user.email.
git init -b main onto-conflict
cd onto-conflict
printf 'timeout=30\nretries=3\nlog=info\n' > app.conf
git add app.conf
git commit -m "Add app.conf"
git switch -c next
printf 'timeout=60\nretries=3\nlog=info\n' > app.conf
git commit -am "Raise timeout to 60"
git switch -c topic
printf 'timeout=60\nretries=3\nlog=info\ncache=on\n' > app.conf
git commit -am "Enable cache"
printf 'timeout=45\nretries=3\nlog=info\ncache=on\n' > app.conf
git commit -am "Tune timeout"
git switch main
printf 'timeout=20\nretries=3\nlog=info\n' > app.conf
git commit -am "Lower timeout on main"
git switch topic
git rebase --onto main next topicThe first commit, "Enable cache", only appends a line, so it replays cleanly. The second commit, "Tune timeout", changes a line that main also changed. Git stops with output like this (your commit hash will differ):
Auto-merging app.conf
CONFLICT (content): Merge conflict in app.conf
error: could not apply 9f3c2e1... Tune timeout
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 hint lines list the three exits. The rest of the chapter explains each one.
Looking at the stopped state
Start with git status:
git statusThe output is similar to this:
interactive rebase in progress; onto 4b7d0a2
Last command done (2 commands done):
pick 1c8e5f0 Enable cache
pick 9f3c2e1 Tune timeout
No commands remaining.
You are currently rebasing branch 'topic' on '4b7d0a2'.
Unmerged paths:
both modified: app.confThree details matter here:
HEADis detached. It points at the last commit that replayed successfully, which is a copy of "Enable cache" on top ofmain. The branch nametopicstill points at the old, unrebased tip until the rebase finishes.- The status lists what is done and what remains.
- Only the files under "Unmerged paths" need your attention. Everything else was merged automatically and is already staged.
To see which commit Git is trying to apply, run:
git rebase --show-current-patchTo list only the conflicted files, run:
git diff --name-only --diff-filter=UResolving the conflict and continuing
Open app.conf. You will see this:
<<<<<<< HEAD
timeout=20
=======
timeout=45
>>>>>>> 9f3c2e1 (Tune timeout)
retries=3
log=info
cache=onThe block above ======= is the version that you are building on: main's timeout=20. The block below is the commit being replayed. Decide what the file should contain, remove the markers, and save. Here the intent of "Tune timeout" was to set 45, so keep that:
printf 'timeout=45\nretries=3\nlog=info\ncache=on\n' > app.conf
git diff --check
git add app.conf
GIT_EDITOR=true git rebase --continuegit add marks the file as resolved. git rebase --continue then creates the commit and moves on to the next one. If no commits remain, Git moves the topic branch name to the new tip and finishes. GIT_EDITOR=true only skips the commit message editor so the script runs without interaction. You can leave it out when working by hand.
Check the result with the range from the earlier chapters:
git log --oneline --graph main topic nexttopic should now sit directly on main, with two new commits that have the same messages as the originals.
Ours and theirs are swapped
During a rebase, the words "ours" and "theirs" mean the opposite of what they mean during a merge. This surprises many people, and it matters whenever you use git checkout --ours, git checkout --theirs, or a merge tool.
| Term | During `git merge` | During `git rebase --onto` |
|---|---|---|
| ours | The branch you are on | The new base plus the commits already replayed |
| theirs | The branch being merged in | The commit currently being replayed |
The reason is that a rebase checks out the new base first and then applies your commits to it. From Git's point of view, you are "on" the new base, and your own work is the incoming change.
So during the conflict above, these two commands pick a whole side:
git checkout --ours app.conf # keep main's version (timeout=20)
git checkout --theirs app.conf # keep the replayed commit's version (timeout=45)After either one, you still need git add app.conf before continuing. These commands replace the entire file, not only the conflicting block, so use them only when you want one side's whole file.
Skipping a commit
git rebase --skip throws away the commit that Git is trying to apply and moves on to the next one. The branch ends up without that commit's changes.
Use it when the commit is no longer needed on the new base. Two common cases are:
- The new base already contains the same change. For example, someone cherry-picked or reapplied your commit on
main, so replaying it conflicts with its own twin. - You resolved the conflict and nothing is left to commit. The new base already has everything the commit did, so there is nothing to record. Git cannot create an empty commit here, and it tells you to skip.
git rebase --skipDo not use --skip to avoid a conflict that you simply find tedious. The result is a branch that silently lacks a change.
Aborting
git rebase --abort cancels the whole operation. Git resets the branch to the commit it pointed at before you started and removes the rebase state. Work you did while resolving conflicts in the current rebase is discarded.
git rebase --abortAbort is safe at any point during a stopped rebase, even after several conflicts you already resolved. If you realize mid-way that the upstream argument was wrong, abort and start again with the right one. A wrong upstream replays too many or too few commits, and fixing it by hand is harder than restarting.
A related command is git rebase --quit. It forgets the rebase state but leaves HEAD and the working tree where they are, without restoring your branch. It is rarely what you want. Prefer --abort.
Reducing repeated conflicts
If you rebase the same branch several times, you may resolve the same conflict repeatedly. Git can remember your resolutions:
git config rerere.enabled trueWith rerere enabled ("reuse recorded resolution"), Git records how you resolved a conflict. If the same conflict appears later, Git applies that resolution for you. You still run git add and git rebase --continue.
A short decision guide
- Run
git statusandgit rebase --show-current-patchto see where Git stopped and what it is applying. - If you want the commit's change, edit the file, check
git diff --check,git addthe file, and rungit rebase --continue. - If the new base already contains the change, or you meant to drop the commit, run
git rebase --skip. - If the range or the base is wrong, or you are lost, run
git rebase --abortand start over.
Exercise
You run git rebase --onto main next topic and Git stops with a conflict. In the conflicted file, the block above ======= shows timeout=20, which is the value on main. Which side is that called, "ours" or "theirs", and which commit does the block below ======= come from?
Show answer
The block above ======= is "ours", because during a rebase "ours" is the new base plus the commits already replayed. The block below comes from "theirs", which is the commit currently being replayed from topic.
Exercise
You resolved a conflict, ran git add, and then git rebase --continue reported that there is nothing to commit because the new base already has the same change. Which command should you run, and what is the cost of running it when you are wrong?
Show answer
Run git rebase --skip. If the new base did not actually contain the change, skipping drops that commit's work from the rebased branch. You can recover the old commit from the reflog, but the new branch will lack the change until you do.
Exercise
Halfway through a rebase you realize you gave next as the upstream when you meant feature-a. What should you do?
Show answer
Run git rebase --abort to restore the branch to its state before the rebase, then rerun the command with the correct upstream. Fixing a wrong range in the middle of a rebase is harder than restarting.