Sub topic 6
Resolving conflicts during merge and rebase
This chapter assumes you can commit, push, and pull, and that you know what a merge and a rebase do. It covers what a conflict is, how to resolve one, and how the process differs between the two commands.
Why conflicts happen
Git merges changes automatically when two branches touch different parts of a file. A conflict occurs when both branches changed the same lines, or when one branch edited a file the other deleted. Git cannot decide which change is correct, so it stops and asks you.
Reading conflict markers
Git writes both versions into the file between markers:
<<<<<<< HEAD
const timeout = 30;
=======
const timeout = 60;
>>>>>>> feature/login-formThe block above ======= is your current side. The block below it is the incoming side. You edit the file to the result you want and delete all three marker lines. A common error is committing a file that still contains markers, so search for <<<<<<< before you continue.
Resolving a merge conflict
git switch main
git merge feature/login-form
# CONFLICT (content): Merge conflict in config.js
git status
# edit config.js, remove the markers
git add config.js
git commitgit status lists the files under "Unmerged paths". Staging a file with git add tells Git it is resolved. When all files are staged, git commit finishes the merge. To abandon the attempt, run git merge --abort.
Resolving a rebase conflict
A rebase replays your commits one at a time, so a conflict can appear once per commit.
git switch feature/login-form
git rebase origin/main
# CONFLICT in config.js
# edit config.js, remove the markers
git add config.js
git rebase --continueUse git commit during a merge, but git rebase --continue during a rebase. To stop and return to the state before you started, run git rebase --abort.
The labels also swap meaning. During a rebase, "HEAD" (the current side) is the branch you are rebasing onto, and the incoming side is your own commit being replayed. This surprises many people.
Comparing the two
| Aspect | Merge | Rebase |
|---|---|---|
| Conflict resolutions | Once, in one merge commit | Possibly once per replayed commit |
| Finish command | `git commit` | `git rebase --continue` |
| Abort command | `git merge --abort` | `git rebase --abort` |
| "Current side" | Your branch | The branch you rebase onto |
Reducing conflicts
- Keep branches short-lived, so they drift less from the main branch.
- Fetch and rebase or merge from main often, so each conflict is small.
- Keep commits focused, and avoid reformatting files you do not otherwise change.
Exercise
You are in the middle of git rebase origin/main and you have fixed and staged a conflicted file. Which command do you run next, and why not git commit?
Show answer
Run git rebase --continue. It records the resolved commit and then replays the next commit. Using git commit would create a commit outside the rebase's control and can leave the rebase in an inconsistent state.
Exercise
During a rebase of your feature branch onto main, the marker block labelled HEAD shows a value you did not write. Whose change is it?
Show answer
It is from main, the branch you are rebasing onto. Your own commit being replayed appears as the incoming side below the ======= line.