Rebasing in Git
Rebase is an alternative to merge that creates a linear history by replaying commits.
What is Rebase?
Rebase moves your branch's commits to start from a different point:
Before rebase:
main: A ─ B ─ C ─ D
\
feature: E ─ F
After rebase:
main: A ─ B ─ C ─ D
\
feature: E' ─ F'
Your commits (E, F) are replayed as new commits (E', F') on top of main.
Basic Rebase
1# Switch to feature branch
2git checkout feature-branch
3
4# Rebase onto main
5git rebase mainRebase vs Merge
Merge
A ─ B ─ C ─────── M
\ /
D ─ E ────
- Preserves history
- Creates merge commit
- Shows branch existed
Rebase
A ─ B ─ C ─ D' ─ E'
- Linear history
- No merge commits
- Cleaner log
When to Use Rebase
✅ Good for Rebase
- Updating feature branch with main
- Cleaning up local commits
- Before merging to main
❌ Avoid Rebase
- On shared/public branches
- After pushing commits
- When history matters
Interactive Rebase
Edit, squash, or reorder commits:
1git rebase -i HEAD~3 # Last 3 commitsInteractive Options
pick abc1234 First commit
pick def5678 Second commit
pick ghi9012 Third commit
# Commands:
# p, pick = use commit
# r, reword = edit message
# e, edit = stop for amending
# s, squash = combine with previous
# f, fixup = squash, discard message
# d, drop = remove commit
Squash Commits
1git rebase -i HEAD~3Change to:
pick abc1234 First commit
squash def5678 Second commit
squash ghi9012 Third commit
Result: Three commits become one.
Reword Commit Message
reword abc1234 First commit
pick def5678 Second commit
Editor opens to change the message.
Rebase Workflow
Update Feature Branch
1# On feature branch
2git checkout feature/my-feature
3
4# Fetch latest main
5git fetch origin main
6
7# Rebase onto main
8git rebase origin/main
9
10# If conflicts, resolve and continue
11git add .
12git rebase --continue
13
14# Force push (required after rebase)
15git push --force-with-lease origin feature/my-featureClean Up Before PR
1# Squash WIP commits
2git rebase -i main
3
4# Make commit history clean
5# Then push
6git push --force-with-leaseHandling Rebase Conflicts
1git rebase main
2# CONFLICT: Resolve in file.txtResolution Steps
1# 1. Fix conflicts in the file
2# 2. Stage the fix
3git add file.txt
4
5# 3. Continue rebase
6git rebase --continue
7
8# 4. Or abort if needed
9git rebase --abortThe Golden Rule of Rebasing
Never rebase commits that have been pushed to a shared repository
Why? Rebase rewrites history. If others have based work on your commits, their history diverges.
Safe Rebase
1# Your own feature branch (not shared)
2git checkout my-feature
3git rebase main
4git push --force-with-lease # OK - your branchDangerous Rebase
1# Never do this on shared branches!
2git checkout main
3git rebase feature # ❌ Don't rebase main!Force Push After Rebase
Rebase changes commit hashes, so you need force push:
1# Regular force (dangerous)
2git push --force
3
4# Safer: fails if remote has new commits
5git push --force-with-leaseAlways use --force-with-lease to avoid overwriting others' work.
Rebase Best Practices
1. Rebase Feature Branches Regularly
1# Keep feature branch updated
2git fetch origin
3git rebase origin/main2. Squash Before Merging
1# Clean up WIP commits
2git rebase -i main
3# Squash related commits together3. Don't Rebase Public History
If commits are shared, use merge instead.
4. Use Meaningful Commit Messages
After squashing, write a good combined message.
Summary
| Command | Description |
|---|---|
git rebase main | Rebase onto main |
git rebase -i HEAD~n | Interactive rebase |
git rebase --continue | Continue after conflict |
git rebase --abort | Cancel rebase |
git push --force-with-lease | Safe force push |
Rebase creates cleaner history but requires care with shared branches!
