Git Rebase
# Rebase current branch onto maingit rebase main
# Interactive rebase (last 3 commits)git rebase -i HEAD~3
# Continue after resolving conflictsgit rebase --continue
# Abort rebasegit rebase --abort
# Force push after rebase (use with caution!)git push --force-with-leaseThe concept
Section titled “The concept”Reapply commits on top of another base tip.
~ Git Documentation
Git rebase rewrites commit history by moving or combining a sequence of commits to a new base commit. This creates a cleaner, linear history compared to merge commits.
How it works
Section titled “How it works”When you rebase, Git:
- Finds the common ancestor between your branch and the target branch
- Saves your commits temporarily
- Resets your branch to the target branch
- Reapplies your commits one by one on top
Before rebase: A -- B -- C (main) \ D -- E (feature)
After git rebase main: A -- B -- C (main) \ D' -- E' (feature)The commits D' and E' have the same changes as D and E, but different
commit hashes.
Basic usage
Section titled “Basic usage”Simple rebase
Section titled “Simple rebase”# Switch to your feature branchgit switch feature-branch
# Rebase onto maingit rebase main
# If there are conflicts, resolve them, then:git add .git rebase --continue
# Push the rebased branch (rewrites history!)git push --force-with-leaseInteractive rebase
Section titled “Interactive rebase”Interactive rebase lets you edit, reorder, squash, or delete commits:
# Rebase the last 5 commits interactivelygit rebase -i HEAD~5
# Or rebase from a specific commitgit rebase -i abc1234This opens an editor with your commits:
pick a1b2c3d Add user authenticationpick d4e5f6g Fix typo in login formpick h7i8j9k Update testspick l0m1n2o Refactor validation logic
# Commands:# p, pick = use commit# r, reword = use commit, but edit message# e, edit = use commit, but stop for amending# s, squash = use commit, but meld into previous commit# f, fixup = like "squash", but discard commit message# d, drop = remove commitCommon operations:
# Squash last two commits togetherpick a1b2c3d Add user authenticationpick d4e5f6g Fix typo in login formsquash h7i8j9k Update tests # Combines with previouspick l0m1n2o Refactor validation logic
# Change commit messagepick a1b2c3d Add user authenticationreword d4e5f6g Fix typo in login form # Will prompt for new messagepick h7i8j9k Update tests
# Delete a commitpick a1b2c3d Add user authenticationdrop d4e5f6g Fix typo in login form # Removes this commitpick h7i8j9k Update testsCommon use cases
Section titled “Common use cases”1. Clean up commits before merging
Section titled “1. Clean up commits before merging”# Clean up your feature branch before PRgit checkout feature-branchgit rebase -i main
# Squash "WIP" and "fix typo" commits# Reword unclear commit messages# Remove debug commits2. Update feature branch with latest main
Section titled “2. Update feature branch with latest main”# Get latest changes from maingit checkout maingit pull
# Update feature branchgit checkout feature-branchgit rebase main3. Fix commit message
Section titled “3. Fix commit message”# Change the last commit messagegit commit --amend
# Change older commit messagesgit rebase -i HEAD~5# Change 'pick' to 'reword' for commits to edit4. Squash multiple commits
Section titled “4. Squash multiple commits”# Squash last 3 commits into onegit rebase -i HEAD~3# Change 'pick' to 'squash' for commits 2 and 3Resolving conflicts
Section titled “Resolving conflicts”When conflicts occur during rebase:
# 1. Git stops and shows conflicting files# 2. Edit files to resolve conflicts# 3. Stage resolved filesgit add .
# 4. Continue the rebasegit rebase --continue
# Or skip the commit causing issuesgit rebase --skip
# Or abort and start overgit rebase --abortImportant commands
Section titled “Important commands”# Abort rebase and return to original stategit rebase --abort
# Skip current commit during rebasegit rebase --skip
# Continue after resolving conflictsgit rebase --continue
# Edit a specific commitgit rebase -i HEAD~3# Change 'pick' to 'edit' for the commit
# Rebase onto a different branchgit rebase --onto main feature-branchRebase vs Merge
Section titled “Rebase vs Merge”| Feature | Rebase | Merge |
|---|---|---|
| History | Linear, clean | Shows true history with merge commits |
| Commits | Rewrites commit hashes | Preserves original commits |
| Conflicts | Resolve per commit | Resolve once |
| Use for | Local cleanup, updating branches | Integrating completed features |
| Team use | ⚠️ Never on public/shared branches | ✅ Safe for shared branches |
The Golden Rule
Section titled “The Golden Rule”Never rebase commits that have been pushed to a shared/public repository.
~ Every Git tutorial ever
Rebasing rewrites history. If others have based work on commits you rebase, you’ll cause serious problems.
✅ Safe to rebase:
- Local commits not yet pushed
- Feature branches only you’re working on
- After coordinating with your team
❌ Never rebase:
- The
mainormasterbranch - Commits others have based their work on
- Public release tags
- Use
--force-with-leaseinstead of--force: It’s safer because it checks if someone else pushed changes - Rebase often: Keep your feature branch up-to-date with main
- Communicate: Tell your team when you rebase shared branches
- Keep backups: Create a backup branch before complex rebases:
git branch backup-branch - Start simple: Practice on local branches before rebasing pushed work
Resources
Section titled “Resources”Official Git documentation on git rebase
Atlassian tutorial on merging vs rebasing
Git SCM book chapter on rebasing
GitHub guide on interactive rebase
Julia Evans’ zine on git rebase