Script Valley
Git and GitHub Complete Course: From Beginner to Advanced
Branching, Merging, and Resolving ConflictsLesson 2.5

Rebasing Branches

git rebase, rebase vs merge, interactive rebase, squash commits, linear history

Rebasing Branches

Rebasing is an alternative to merging that rewrites commit history to create a linear sequence. Instead of creating a merge commit, rebase replays your branch's commits on top of the target branch as if you had branched from the latest point.

DiagramRebase vs Merge History Comparison

IMAGE PROMPT (replace this block with your generated image):

Flat two-track commit graph on white background. Title: git rebase vs git merge: History Comparison. Top track: Before (diverged branches). Main: C1 → C2 → C3. Feature branching from C2: C2 → F1 → F2. Both tracks shown with labeled badges. Middle section: two outcome columns. Left outcome: After git merge. Shows C1 → C2 → C3 → M (merge commit). F1 and F2 connect diagonally into M. M has two parent arrows. Label: Preserves full history, shows true branching. Right outcome: After git rebase. Shows C1 → C2 → C3 → F1' → F2' (all linear). F1' and F2' are new commits (prime notation). Label: Linear history, commits rewritten (new hashes). Warning badge below rebase outcome: Golden Rule: Never rebase pushed commits — red warning icon. Interactive rebase box: git rebase -i HEAD~3 → pick / squash / reword / drop pills. Brand color #3A5EFF for rebase track. White background.

Basic Rebase

git switch feature/payment
git rebase main

This takes all commits on feature/payment that are not in main, and replays them on top of main. The result is a clean, linear history without merge commits.

Rebase vs Merge

Merge preserves the true history of when branches diverged and were combined. Rebase produces a cleaner linear history but rewrites commits (changing their hashes). The golden rule: never rebase commits that have been pushed to a shared remote repository, as this forces others to reconcile diverged histories.

Interactive Rebase

Interactive rebase is one of the most powerful Git features. It allows you to edit, reorder, squash, or delete commits before pushing:

git rebase -i HEAD~3

This opens an editor with the last 3 commits. Change pick to squash to combine commits, reword to change a message, or drop to remove a commit entirely.

When to Use Rebase

Use rebase to clean up local work before pushing, to update a feature branch with the latest main branch changes, or to squash multiple work-in-progress commits into one clean commit for review.