๐ Rebase Operations โ
What is Rebase? โ
If merge is "converging two roads into one," then rebase is "splicing your road onto the end of someone else's road." It "replays" the commits from your branch onto the latest position of the target branch.
Before Rebase:
C---D (feature)
/
A---B---E---F (main)
After Rebase:
C'---D' (feature)
/
A---B---E---F (main)
Note: C' and D' are brand new commits (hashes changed), not the original C and DBasic Rebase Operation โ
# Scenario: You're developing on a feature branch, and main has new commits
# You want to "move" your feature branch commits to after main's latest commit
git switch feature/product-details
# Rebase current branch onto main
git rebase main
Successfully rebased and updated refs/heads/feature/product-details.
# After rebase, switch back to main and merge (this will be a fast-forward)
git switch main
git merge feature/product-detailsInteractive Rebase โ Organizing Commit History โ
Interactive Rebase is one of Git's most powerful features. It lets you modify, merge, delete, and reorder commits. It's like "editing" your commit history.
# Interactive rebase on the last 3 commits
git rebase -i HEAD~3
# This opens the editor showing something like:
pick a1b2c3d feat: add product listing page
pick b2c3d4e fix: fix price display issue
pick c3d4e5f feat: add product details page
# Rebase f4e5d6c..c3d4e5f onto f4e5d6c (3 commands)
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# d, drop = remove commit
#
# These lines can be re-ordered; they are executed from top to bottom.# Squash commits:
pick a1b2c3d feat: add product listing page
squash b2c3d4e fix: fix price display issue
pick c3d4e5f feat: add product details page
# Save and exit, Git will let you edit the combined commit message๐ก Tip: Common uses for Interactive Rebase:
- Combine trivial commits: merge "fix typo", "fix again", "actually fixed now" into one meaningful commit
- Edit commit messages: use
rewordto rewrite commit messages- Delete useless commits: use
dropto remove debug code commits- Reorder commits: change the order of commits
The Golden Rule of Rebase โ
โ ๏ธ Warning: โ ๏ธ Never rebase commits that have been pushed to a public branch! Why? Because rebase changes commit hashes (effectively creating brand new commits). If you've already pushed to remote and your teammates have been working based on those commits, your rebase + push will cause their local history and remote history to "diverge," creating a bunch of conflicts. Remember: rebasing local unpushed commits = good habit; rebasing already-pushed commits = chaos
Rebase vs Merge Comparison โ
| Comparison | Merge | Rebase |
|---|---|---|
| History | Preserves full history (including branch structure) | Linear history, cleaner |
| Commit hashes | Unchanged | Changed |
| Merge commit | Produces an extra merge commit | No merge commit needed |
| Safety | Safer, doesn't modify history | Already-pushed commits cannot be rebased |
| Use case | Merging public branches, preserving full history | Syncing with main branch, organizing local commits |
| Conflict resolution | Resolve all conflicts at once | May need to resolve conflicts commit by commit |