The Hook (The "Byte-Sized" Intro)
A branch that's 3 days behind main merges smoothly. A branch that's 3 weeks behind merges with tears. The longer you wait to sync, the more your code diverges, the more conflicts pile up, and the more painful the eventual merge becomes. The fix is simple: update your branch daily. Two minutes of syncing saves two hours of conflict resolution.
📖 What is Keeping Branches Up to Date?
Keeping a branch up-to-date means regularly incorporating the latest changes from the base branch (usually main) into your feature branch. This prevents large divergence and reduces merge conflicts.
Conceptual Clarity
Two strategies:
| Strategy | Command | What It Does | History Shape |
|---|---|---|---|
| Merge main in | git merge main | Adds a merge commit pulling in main's changes | Branched (preserves reality) |
| Rebase onto main | git rebase main | Replays your commits on top of main's latest | Linear (cleaner log) |
When to update:
- Daily, as part of your morning routine
- Before opening a PR (to ensure a clean merge)
- When
mainreceives a big change (to surface conflicts early) - When CI reports your branch is behind
Real-Life Analogy
Updating your branch is like syncing your study notes with classmates daily. If you sync every evening, discrepancies are tiny. If you wait until exam week, you're drowning in conflicting notes from 6 different people.
Visual Architecture
Why It Matters
- Smaller conflicts: Frequent syncs keep diffs small and manageable.
- CI stays green: Your branch tests against the latest shared code, not stale code.
- Faster merges: An up-to-date branch merges instantly — no last-minute firefighting.
- Team confidence: Reviewers trust a branch that's current with
main.
Code
# ─── Strategy 1: Merge main into your branch ───
git switch feature/dashboard
git fetch origin
git merge origin/main
# Resolve any conflicts, then continue working
# ─── Strategy 2: Rebase onto main (cleaner) ───
git switch feature/dashboard
git fetch origin
git rebase origin/main
# If conflicts arise during rebase:
git add resolved-file.js
git rebase --continue
# After rebase, force-push (since history changed):
git push --force-with-lease
# ─── Automation: set pull to rebase by default ───
git config --global pull.rebase true
# ─── Quick check: how far behind am I? ───
git rev-list --count main..feature/dashboard
# Output: 0 (you're caught up!)
git rev-list --count feature/dashboard..main
# Output: 5 (main has 5 commits you don't have)Merge vs Rebase for Updates
| Aspect | Merge from main | Rebase onto main |
|---|---|---|
| History | Adds merge commits (noisy but honest) | Clean, linear (commits replayed) |
| Safety | ✅ Never rewrites history | ⚠️ Rewrites history (needs force-push) |
| Conflict resolution | Once per merge | Potentially once per replayed commit |
| Best for | Shared branches, cautious approach | Solo feature branches, clean PRs |
Key Takeaways
- Update your branch daily — small syncs prevent big conflicts.
- Use
git merge origin/mainfor safety,git rebase origin/mainfor clean history. - After rebasing, use
git push --force-with-lease(never bare--force). - Check how far behind you are with
git rev-list --count.
Interview Prep
-
Q: Why should you keep a feature branch updated with main? A: To minimize merge conflicts by preventing large divergence. An up-to-date branch surfaces conflicts when they're small (1-2 lines) instead of when they've grown into 50-line nightmares.
-
Q: Should you merge or rebase to update your branch? A: Both are valid. Merging is safer (no history rewriting) but adds merge commits. Rebasing creates a cleaner, linear history but requires force-pushing. For solo feature branches, rebase is common. For shared branches, merge is safer.
-
Q: What happens if you forget to update your branch before opening a PR? A: The PR may show conflicts that prevent merging. You'll need to update your branch (merge or rebase), resolve any conflicts, and push again. Many CI systems will also fail if the branch is too far behind main.