Merge, Rebase, and Conflicts
Two ways to integrate history: preserve it with merge, or rewrite it linear with rebase โ and how to survive conflicts either way.
Two branches diverged. How do you put them back together?
git merge: preserve history
git checkout feature
git merge main
# or, from main:
git merge feature # creates a merge commit if histories diverged
A merge commit has two parents. History shows exactly what happened: "these two lines of work were combined here." Non-destructive โ safe on shared branches.
git rebase: rewrite into a straight line
git checkout feature
git rebase main
Rebase replays your commits on top of the latest main, creating new commit objects (same changes, new IDs). Result: linear history, no merge commit. The cost: the old commits are abandoned โ which is why the golden rule exists.
Golden rule: never rebase commits others have pulled. Rebase rewrites IDs; everyone's copies of the "same" commits become duplicates. Rebase only commits that exist only on your machine (or coordinate explicitly).
The professional pattern:
# keep feature fresh without a noise merge commit
git fetch origin
git rebase origin/main
Conflicts: a resolution algorithm, not a panic
A conflict happens when both branches changed the same lines. Git marks the file:
<<<<<<< HEAD
const total = subtotal * 1.1; // your side
=======
const total = subtotal + fee; // incoming side
>>>>>>> main
Resolution procedure:
git statusโ list every conflicted file (resolve all of them)- Open each file; decide the correct code for the product โ not "mine" or "theirs" reflexively; often the answer is a combination
- Remove all three marker lines
git add <file>to mark resolved- Finish:
git merge --continueorgit rebase --continue - Run the tests before finishing โ a conflict resolved textually can still be broken logically
Escape hatches: git merge --abort / git rebase --abort return you to the exact pre-operation state. During a rebase conflict loop, git rebase --skip drops the offending commit entirely.
Which to use?
- Shared/public branches โ merge (never rewrite what others have)
- Your own local feature branch โ rebase onto main for a clean pull request
- Team convention decides the rest โ consistency beats ideology