Git Merge vs Rebase: A Visual Guide
Git history tells a story. Merge is a non-fiction documentary (Messy, real/chronological). Rebase is a edited movie script (Clean, linear story).
Merge (git merge main)
Creates a "Merge Commit". Graph looks like a railway track. Pros: Non-destructive. True history. Cons: Cluttered history with "Merge branch 'main'" commits.
Rebase (git rebase main)
Moves your commits to the TOP of the main branch. "Pretend I started my work today." Graph is a straight line.
The Golden Rule
NEVER rebase a public branch. If others are working on your branch, rebase changes the history (hashes). Their code will break. Only rebase your private feature branch before merging to main.
How we do it
- Working on
feature-A. mainupdates.git checkout feature-Agit pull --rebase origin main(Pull main and replay my work on top)- Resolve conflicts once.
git push --force-with-lease
Result: A beautiful, linear history.