Sub topic 13
Conflicts during rebase
This chapter assumes you can create branches, commit, and resolve a merge conflict by hand as covered earlier. Here you will see how conflicts differ when you rebase instead of merge.
Why rebase conflicts feel different
git rebase main takes the commits on your branch and replays them one at a time on top of main. Each replayed commit can conflict. A branch with five commits can therefore stop up to five times, and you resolve each stop separately. A merge, by contrast, stops once and gives you one combined result.
Resolving a rebase conflict
Using the config.txt example from earlier, rebase feature onto main:
git switch feature
git rebase main
# fix the conflicted files, then:
git add config.txt
git rebase --continue
# or give up and return to the original state
git rebase --abortWhen the conflict appears, Git prints which commit it was applying. Open the file, edit away the markers, and run git add. Do not run git commit. Use git rebase --continue, which creates the replayed commit using the original message (it may open your editor to confirm it). If the next commit conflicts too, repeat the same steps.
git status shows "rebase in progress" and tells you which commit is being applied. git ls-files -u and git diff work as they do during a merge.
Ours and theirs swap meaning
During a rebase, Git starts from main and applies your commits onto it. So the roles reverse:
| Command | During a merge | During a rebase |
|---|---|---|
| `--ours` | The branch you are on | The branch you are rebasing onto (`main`) |
| `--theirs` | The branch being merged | The commit being replayed (your work) |
Taking your own change during a rebase therefore uses --theirs:
git checkout --theirs -- config.txt
git add config.txt
git rebase --continueThe conflict markers follow the same rule. The HEAD section shows the upstream code plus the commits already replayed, and the other section shows the commit being applied.
Useful options
git rebase --abortrestores the branch to its state before the rebase began.git rebase --skipdrops the commit currently being replayed. Use it only when its change is already present upstream or is no longer wanted.git rebase --continuefails if files are still unmerged or unstaged. Rungit statusto find what is left.
Exercise
You run git rebase main and Git stops with a conflict in app.py. You want to keep your own branch's version of the file for this commit. Which checkout option do you use, and what are the next two commands?
Show answer
Use git checkout --theirs -- app.py, because during a rebase "theirs" is the commit being replayed from your branch. Then run git add app.py and git rebase --continue.