B · CollaborateIntermediate45 min read
6. Rebase vs merge & history hygiene
Git & GitHub · 48 pages source format
Merge preserves branch topology. Rebase replays commits for linear history. Never rebase pushed shared branches without team agreement.
What you'll learn
- git merge
- git rebase
- Interactive rebase lite
- When to use each
Merge feature into main
git checkout main && git merge feature/x — creates merge commit if diverged.
Do this now
Learn Git Branching: merge levels. Replay in git-practice.
Clear?
Rebase onto main
git switch feature && git rebase main — replays your commits on top of latest main. Cleaner log.
git switch main
git pull
git switch feature/login
git rebase main
# fix conflicts if any
git add .
git rebase --continueDo this now
Rebase feature branch onto updated main. Resolve conflict if prompted.
Pro tip. Rule: rebase local branches; merge (or squash-merge) into shared main.
Clear?
Interactive rebase
git rebase -i HEAD~3 — squash fixup commits, reword messages. Only on unpushed history.
Do this now
Squash 2 WIP commits into one before opening PR.
Clear?