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.

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.