Sub topic 7
Handling Conflicts: Continue, Skip, and Abort
This chapter assumes you can commit, branch, and read git log. It also assumes you know the shape of git rebase --onto <newbase> <upstream> <branch> from the earlier chapters.
Why a rebase stops
A rebase copies commits one at a time. Git applies each commit's changes on top of the new base. If the new base changed the same lines, Git cannot combine the two edits. It stops and asks you to decide.
Your branch is in the middle of an operation at this point. git status will say "rebase in progress" and list the conflicted files. Nothing is lost. The original branch is still reachable, and you can leave at any time.
Resolving and continuing
Suppose you run this and Git stops:
git rebase --onto main next topicFollow these steps.
- Run
git status. Files under "Unmerged paths" have conflicts. - Open each file and find the markers
<<<<<<<,=======, and>>>>>>>. Edit the file so it holds the final content you want, and delete the markers. - Stage each resolved file with
git add. - Run
git rebase --continue.
git status # see the conflicted files
# edit the files to resolve the conflict markers
git add path/to/file
git rebase --continueGit then makes the commit with your resolution and moves to the next one. Another conflict can appear on a later commit. Repeat the same steps each time.
Do not run git commit yourself to finish a step. Use --continue, so Git can record its progress and move on.
Skipping a commit
Sometimes the commit that conflicts is no longer needed. For example, the new base may already contain the same fix. In that case, resolving the conflict would leave an empty commit.
git rebase --skip--skip throws away the commit that is currently being replayed and moves on. That commit will not appear on the rewritten branch. Use it only when you are sure its changes are not needed. If you skip the wrong commit, you can recover it using the steps in the recovery chapter.
If you resolve a conflict and the result is identical to the base, Git may report that there is nothing to commit. Running git rebase --skip is the usual answer in that case.
Aborting
If the conflicts look wrong, stop and go back:
git rebase --abortThis returns the branch to the state it had before you started the rebase. Your working tree and branch pointer are restored. Abort is a good choice when you suspect you gave the wrong upstream, because a wrong range often produces many unexpected conflicts. Preview the range with git log --oneline <upstream>..<branch>, fix the command, and start again.
Choosing between the three commands
| Situation | Command |
|---|---|
| You fixed the files and want to proceed | `git add` then `git rebase --continue` |
| The current commit is redundant or unwanted | `git rebase --skip` |
| The rebase is going wrong, or you want to rethink | `git rebase --abort` |
Common errors
- Running
git rebase --continuewith unstaged resolutions. Git refuses and tells you to add the files first. - Leaving conflict markers in a file and staging it. Git does not check the content, so the markers get committed. Search for
<<<<<<<before you continue. - Forgetting that you are mid-rebase. If
git statusshows a detachedHEADand a "rebase in progress" message, finish with one of the three commands above.
Exercise
During git rebase --onto main next topic, Git stops on a conflict. You open the file and see that the base already contains the same fix your commit makes, so your commit is redundant. Which command should you run, and what happens to that commit?
Show answer
Run git rebase --skip. Git discards the commit being replayed, so it does not appear on the rewritten topic, and the rebase moves on to the next commit. If you were unsure, git rebase --abort would restore topic to its state before the rebase.