Lesson 96 of 296
Merge conflicts; rebase vs merge
What you will learn
- Why conflicts happen
- Reading conflict markers
- Resolving a conflict step by step
- What rebase does
- Interactive rebase to tidy commits
- The golden rule of rebasing
- Force pushing safely
- Merge vs rebase: when to use each
On this page
Why conflicts happen
A merge conflict happens when two branches change the same lines of the same file differently, or one branch edits a file that the other deleted. Git cannot know which version is right, so it asks you.
Conflicts are normal. They are not a sign that you did something wrong.
What a conflict looks like
git merge feature/discount
CONFLICT (content): Merge conflict in src/price.js
Automatic merge failed; fix conflicts and then commit the result.Inside src/price.js:
function finalPrice(pricePence) {
<<<<<<< HEAD
return Math.round(pricePence * 1.2);
=======
return applyDiscount(pricePence) * 1.2;
>>>>>>> feature/discount
}| Marker | Meaning |
|---|---|
<<<<<<< HEAD | Start of your current branch's version |
======= | The divider |
>>>>>>> feature/discount | End of the other branch's version |
Resolving it, step by step
- Run
git statusto see every file with conflicts ("both modified"). - Open each file and find the markers.
- Decide the correct result. It may be one side, the other side, or a combination of both. Here both changes matter:
function finalPrice(pricePence) {
return Math.round(applyDiscount(pricePence) * 1.2);
}- Delete all the markers.
- Run the code and the tests. A conflict-free file can still be logically broken.
- Stage and finish:
git add src/price.js
git commitTo give up and go back to how things were before the merge:
git merge --abortVS Code and other editors show conflicts with buttons such as "Accept Current", "Accept Incoming" and "Accept Both". They help, but you must still understand the code. Talk to the teammate who wrote the other change if you are unsure.
Reducing conflicts
- Keep branches short-lived.
- Pull
maininto your branch (or rebase) often. - Keep PRs small.
- Agree on a formatter (such as Prettier), so formatting changes do not cause conflicts.
What rebase does
Rebase moves your branch's commits so they start from the latest commit of another branch. Git replays your commits, one by one, on top.
Before:
A - B - D - E <- main
\
C - F <- feature
git switch feature
git rebase main
After:
A - B - D - E <- main
\
C' - F' <- featureC' and F' contain the same changes as C and F, but they are new commits with new hashes, because their parent changed.
The result is a linear history, as if you had started your work from the latest main.
If a replayed commit conflicts, Git stops. Fix the files, then:
git add <file>
git rebase --continueOr cancel the whole rebase with git rebase --abort.
Interactive rebase: tidy your commits
Before opening a PR, you can clean up messy commits ("wip", "fix typo", "oops"):
git rebase -i HEAD~4Git opens a list of the last 4 commits. Change the word at the start of each line:
| Word | Effect |
|---|---|
pick | Keep the commit as it is |
reword | Keep it, but edit the message |
squash | Combine with the commit above, and edit the combined message |
fixup | Combine with the commit above, and discard this message |
drop | Delete the commit |
You can also reorder the lines to reorder the commits.
The golden rule of rebasing
Never rebase commits that other people have already pulled.
Rebasing creates new commits with new hashes. If teammates built work on the old commits, their history no longer matches yours, which causes duplicated commits and painful conflicts.
Safe: rebasing your own feature branch that nobody else works on.
Not safe: rebasing main or a shared branch.
Force pushing
After rebasing a branch you already pushed, a normal push is rejected, because the history changed. You must force push:
git push --force-with-leaseAlways use --force-with-lease instead of --force. It refuses to overwrite the remote branch if someone else pushed to it since your last fetch. Plain --force silently deletes their work.
Merge vs rebase
| Merge | Rebase | |
|---|---|---|
| History | Keeps the real history, including when branches split and joined | Rewrites your commits into a straight line |
| Creates new commits | One merge commit | New copies of every rebased commit |
| Safe on shared branches | Yes | No |
| Conflicts | Resolved once, in the merge commit | May need resolving once per replayed commit |
| History readability | Can become cluttered with merge commits | Clean and linear |
Common team practice:
- Use rebase (or
git pull --rebase) to update your own feature branch with the latest main, and interactive rebase to tidy commits before review. - Use merge (often squash and merge on GitHub) to bring a finished PR into main.
- Never rebase shared branches.
Many developers set git config --global pull.rebase true so git pull rebases local commits instead of creating small merge commits.
Common mistakes
- Committing with conflict markers still in the file.
- Choosing one side without understanding the other change.
- Rebasing a shared branch.
- Using
--forceinstead of--force-with-lease. - Panicking during a rebase.
git rebase --abortalways takes you back to the start.
How to say it in an interview
"A conflict happens when both branches change the same lines. I check git status, open each file, decide the correct combined result, remove the markers, run the tests, then stage and commit; git merge --abort cancels if needed. Rebase replays my commits on top of another branch, creating new commits and a linear history. I rebase my own feature branch to keep it up to date and use interactive rebase to squash fixup commits, but never rebase shared history, and I force push with force-with-lease. Merge preserves the true history and is safe on shared branches."
Key words
| Word | Meaning |
|---|---|
| Merge conflict | Git cannot combine changes automatically |
| Conflict markers | <<<<<<<, =======, >>>>>>> |
| Rebase | Replaying commits on top of another branch |
| Interactive rebase | Editing, combining or reordering commits |
| Squash | Combining several commits into one |
| Linear history | A history with no branching lines |
| Force push | Overwriting remote history |
--force-with-lease | A safer force push that protects others' work |
Now prove it
This lesson is complete when you pass the quiz and all three practice levels.