Skip to content

๐Ÿ“ 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.

text
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 D

Basic Rebase Operation โ€‹

bash
# 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-details

Interactive 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.

bash
# 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.
text
# 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 reword to rewrite commit messages
  • Delete useless commits: use drop to 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 โ€‹

ComparisonMergeRebase
HistoryPreserves full history (including branch structure)Linear history, cleaner
Commit hashesUnchangedChanged
Merge commitProduces an extra merge commitNo merge commit needed
SafetySafer, doesn't modify historyAlready-pushed commits cannot be rebased
Use caseMerging public branches, preserving full historySyncing with main branch, organizing local commits
Conflict resolutionResolve all conflicts at onceMay need to resolve conflicts commit by commit