Sub topic 13
Resolve conflicts and rebase onto upstream
This chapter assumes you can commit and push on a feature branch, and that your fork has an upstream remote. It also assumes you have opened a pull request. Here you will handle the case where upstream changes under your open pull request.
Why conflicts appear
While your pull request is open, maintainers keep merging other work into upstream main. If someone changes the same lines you changed, Git cannot decide which version to keep. GitHub then shows "This branch has conflicts that must be resolved" on your pull request. Many projects also ask you to rebase even when there is no conflict, so the history stays linear.
Rebasing replays your commits one at a time on top of the latest upstream main. Your commits get new hashes because their parent changes. A conflict can stop the replay at any of your commits.
Start the rebase
Fetch first, then rebase your branch onto upstream.
git fetch upstream
git switch fix-readme-typo
git rebase upstream/mainIf a conflict occurs, Git stops and prints which files conflict. Run git status to see them listed under "Unmerged paths".
Read the conflict markers
Git writes both versions into the file:
<<<<<<< HEAD
Run `npm ci` to install dependencies.
=======
Run `npm install` to install dependencies.
>>>>>>> 3f2a1c9 (Fix typo in README installation section)During a rebase, the part above ======= is the upstream version. The part below is your commit being replayed. This is the reverse of what many people expect from a merge.
Edit the file so it contains the text you want. Delete all three marker lines. You may keep one side, the other side, or a combination.
Finish the rebase
- Save the file and check that no markers remain.
- Stage the file with
git add README.md. - Continue with
git rebase --continue. - Repeat if Git stops on another commit.
- Run the project's tests or build to confirm the result works.
Then update your pull request. The rewritten commits no longer match the ones on your fork, so a plain push is rejected.
git push --force-with-lease origin fix-readme-typo--force-with-lease refuses the push if the remote branch has commits you have not seen. Plain --force has no such check.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Rebase stops with "CONFLICT" | Same lines changed upstream | Edit the file, `git add`, `git rebase --continue` |
| `git push` rejected after rebase | Local history was rewritten | Use `git push --force-with-lease origin BRANCH` |
| Markers still in the file after continuing | Added the file without removing markers | Search for `<<<<<<<`, fix, and commit a correction |
| You are unsure about the result | Resolution went wrong | Run `git rebase --abort` to return to the state before the rebase |
Exercise
During a rebase, Git stops with a conflict in README.md. You fixed the file. List the commands you run next to finish the rebase and update your pull request, assuming your branch is fix-readme-typo.
Show answer
Run git add README.md, then git rebase --continue. Repeat if Git stops again. When the rebase finishes, run git push --force-with-lease origin fix-readme-typo. The force is needed because the rebase rewrote your commits.
Exercise
In a rebase conflict, which side of the ======= line holds your own change?
Show answer
The part below =======, ending at the >>>>>>> line with your commit's message. The part above, under <<<<<<< HEAD, is the upstream code you are rebasing onto.