Sub topic 8
Handling conflicts: continue, skip and abort
This chapter assumes you can start an interactive rebase and edit the todo list. It also assumes you can read a merge conflict in a file. You should know git add, git status and git commit.
Why a rebase hits conflicts
A rebase replays each commit in the todo list, one at a time, on top of the new base. Replaying a commit means applying its diff to a tree that may differ from the one it was first written against. If the surrounding lines have changed, Git cannot apply the diff on its own. It stops and asks you to decide.
Conflicts show up most often after you reorder commits, drop a commit that later commits depend on, or squash commits that touch the same lines.
Recognizing a stopped rebase
When a conflict occurs, Git prints the name of the commit that failed and the files in conflict. Run git status to see where you are:
git statusThe output says interactive rebase in progress and lists the files under Unmerged paths. It also tells you how many commits are done and how many remain. Your branch is in a detached state during this time. This is normal.
Conflict markers appear in the files:
<<<<<<< HEAD
const label = "Sign in";
=======
const label = "Log in";
>>>>>>> 3f2a1b9 (Add login form)During a rebase the sides can look reversed. HEAD (the top section) is the work already replayed onto the new base. The bottom section is the commit being applied right now. Your own commit is the one being applied, not HEAD.
Continue: resolve and move on
To resolve a conflict, edit the file so it holds the result you want and remove the markers. Then stage the file and continue:
git add src/login.js
git rebase --continueDo not run git commit yourself for a plain conflict. --continue creates the commit using the original message. If the message needs changes, Git opens your editor. If you forget git add, Git refuses to continue and reports unmerged files.
A later commit may conflict as a result of how you resolved an earlier one. Repeat the cycle until the rebase finishes.
Skip: drop the commit that cannot apply
Sometimes the change in a commit already exists in the new base. This happens when a teammate made the same fix upstream. Resolving then leaves an empty commit. In this case, skip it:
git rebase --skip--skip discards the current commit from the rewritten series. The effect matches drop in the todo list, but you decide while the rebase is running. Do not skip a commit just to get rid of a hard conflict. Its changes will be missing from the result, and later commits may then fail or silently lose behavior.
Abort: return to where you started
If the conflicts show that your plan was wrong, go back:
git rebase --abortYour branch returns to the state it had before git rebase -i began. Aborting is safe even after you have resolved several conflicts. The resolved work is discarded, so abort only if you are willing to repeat it.
Choosing between the three
| Situation | Command | Result |
|---|---|---|
| Conflict resolved and files staged | `git rebase --continue` | Commit is created, rebase moves on |
| Change already exists upstream | `git rebase --skip` | Commit is left out |
| Todo list or base was wrong | `git rebase --abort` | Branch returns to its original state |
Exercise
You are in the middle of a rebase. A commit conflicts, and git status shows that the same change was already merged into the base by a colleague. What do you run, and why not git rebase --continue?
Show answer
Run git rebase --skip. The commit's change already exists in the base, so applying it would produce an empty or duplicate commit. --continue is for commits whose conflicts you resolved and staged. Here there is nothing worth keeping from the commit.
Exercise
During a rebase, the conflict markers show your new code in the bottom section, under the >>>>>>> line, and the other code under HEAD. Is this a mistake?
Show answer
No. During a rebase, HEAD points to the commits already replayed onto the new base. The bottom section holds the commit being applied, which is your own work, so the sides appear reversed compared with a merge.