Git Rebase Complete Guide: Easy and Advanced Examples
Git Rebase Complete Guide: Easy and Advanced Examples
Git rebase is one of the most powerful yet misunderstood Git features. Many developers avoid it, but understanding rebase can transform your Git workflow, creating cleaner commit histories and making collaboration smoother. This comprehensive guide covers everything from basic rebase operations to advanced interactive rebasing.
What is Git Rebase?
Rebase rewrites commit history by moving or combining commits. Unlike merge, which creates a merge commit, rebase replays your commits on top of another branch, creating a linear history.
Rebase vs Merge
Merge:
Rebase:
When to Use Rebase
✅ Use rebase when:
❌ Avoid rebase when:
Easy Rebase Examples
Example 1: Updating Feature Branch with Main
Scenario: You're working on a feature branch, and main has new commits. You want to include those changes.
1# Current situation:2# main: A---B---C---D3# feature: E---F4 5# Step 1: Switch to your feature branch6git checkout feature7 8# Step 2: Rebase onto main9git rebase main10 11# Result:12# main: A---B---C---D13# feature: E'---F'What happens:
1. Git finds the common ancestor (commit B)
2. Temporarily removes your commits (E, F)
3. Applies main's new commits (C, D)
4. Replays your commits on top (E', F')
Real-world example:
1# You're working on a login feature2git checkout login-feature3 4# Main branch has security updates you need5git rebase main6 7# Your login commits are now on top of security updatesExample 2: Simple Rebase with Conflict Resolution
Scenario: Rebase encounters conflicts that need resolution.
1# Start rebase2git checkout feature3git rebase main4 5# If conflicts occur:6# Git will pause and show:7# CONFLICT (content): Merge conflict in app.js8 9# Step 1: Open conflicted file and resolve10# app.js will have conflict markers:11<<<<<<< HEAD12// Code from main branch13const apiUrl = 'https://api.example.com';14=======15// Code from your feature branchAborting rebase:
1# If you want to cancel the rebase2git rebase --abort3# Returns to state before rebase startedExample 3: Rebasing Last N Commits
Scenario: You want to rebase only the last 3 commits.
1# Interactive rebase for last 3 commits2git rebase -i HEAD~33 4# Opens editor with:5pick abc123 First commit6pick def456 Second commit7pick ghi789 Third commit8 9# You can now:10# - reword: Change commit message11# - edit: Modify commit12# - squash: Combine with previous13# - drop: Remove commitAdvanced Rebase Examples
Example 1: Interactive Rebase - Squashing Commits
Scenario: You have multiple small commits and want to combine them into meaningful commits.
1# Current commits (messy):2# abc123 Add button3# def456 Fix typo4# ghi789 Style button5# jkl012 Add click handler6# mno345 Fix bug7 8# Start interactive rebase9git rebase -i HEAD~510 11# Editor opens:12pick abc123 Add button13pick def456 Fix typo14pick ghi789 Style button15pick jkl012 Add click handlerInteractive rebase commands:
Example 2: Editing a Specific Commit
Scenario: You need to add a file to a previous commit.
1# Start interactive rebase2git rebase -i HEAD~33 4# Editor shows:5pick abc123 Add login form6pick def456 Add validation7pick ghi789 Add error handling8 9# Change to edit the first commit:10edit abc123 Add login form11pick def456 Add validation12pick ghi789 Add error handling13 14# Save and close. Git stops at that commit:15# Stopped at abc123... Add login formExample 3: Complex Rebase with Multiple Branches
Scenario: You have a feature branch based on another feature branch, and both need to be rebased.
1# Initial state:2# main: A---B---C3# feature-1: D---E4# feature-2: F---G5 6# Step 1: Rebase feature-1 onto main7git checkout feature-18git rebase main9# Result: feature-1 is now A---B---C---D'---E'10 11# Step 2: Rebase feature-2 onto feature-112git checkout feature-213git rebase feature-114# Result: feature-2 is now A---B---C---D'---E'---F'---G'15 Example 4: Rebase with Reflog Recovery
Scenario: You made a mistake during rebase and lost commits. Use reflog to recover.
1# You accidentally did: git rebase --abort (wrong branch)2# Lost your commits!3 4# Step 1: View reflog to find lost commits5git reflog6 7# Output shows:8# abc123 HEAD@{0}: rebase: abort9# def456 HEAD@{1}: commit: Your important work10# ghi789 HEAD@{2}: commit: Previous work11# jkl012 HEAD@{3}: checkout: moving from main to feature12 13# Step 2: Create branch from lost commit14git checkout -b recovered-feature def45615 Example 5: Rebase Onto Specific Commit
Scenario: You want to rebase onto a specific commit, not a branch.
1# Rebase current branch onto specific commit2git rebase --onto main abc1233 4# This means:5# - Take commits after abc1236# - Replay them on top of main7 8# Useful when you want to exclude some commitsAdvanced Interactive Rebase Workflow
Complete Example: Cleaning Up Feature Branch
1# You have messy commits:2git log --oneline3# a1b2c3d Fix typo in comment4# e4f5g6h WIP: trying something5# h7i8j9k Actually, revert that6# j0k1l2m Add feature X7# k3l4m5n Fix bug in feature X8# l6m7n8o Add tests for feature X9# m9n0o1p Update documentation10 11# Step 1: Start interactive rebase12git rebase -i HEAD~713 14# Editor opens. Organize commits:15pick j0k1l2m Add feature XRebase Best Practices
1. Always Rebase Before Pushing
1# Good workflow:2git checkout feature3git rebase main # Update with latest main4git push origin feature5 6# Avoid:7git push origin feature8git rebase main # Don't rebase after pushing!9git push --force # Dangerous!2. Use --autostash for Uncommitted Changes
1# If you have uncommitted changes:2git rebase --autostash main3 4# Git will:5# 1. Stash your changes6# 2. Perform rebase7# 3. Reapply your changes3. Test After Rebase
1# Always test after rebase2git rebase main3npm test # Run your tests4# If tests pass, you're good!4. Communicate with Team
If you must rebase a shared branch:
1. Notify your team
2. Coordinate the rebase
3. Everyone should pull with --rebase after
Common Rebase Scenarios
Scenario 1: Keeping Feature Branch Updated
1# Regular workflow to keep feature branch fresh2git checkout main3git pull origin main4git checkout feature5git rebase main6git push origin feature --force-with-leaseScenario 2: Preparing for Pull Request
1# Clean up before creating PR2git checkout feature3git rebase -i main # Interactive rebase4# Squash, reword, organize commits5git push origin feature --force-with-lease6# Create PR with clean historyScenario 3: Fixing Commit in Middle of History
1# You need to fix commit 3 commits ago2git rebase -i HEAD~43 4# Change:5pick abc123 Commit 16pick def456 Commit 27edit ghi789 Commit 3 (needs fix)8pick jkl012 Commit 49 10# Git stops at commit 311# Make your fixes12git add .13git commit --amend14git rebase --continueTroubleshooting Rebase
Problem: Rebase Conflict
Solution:
1# Resolve conflicts in files2git status # See conflicted files3# Edit files to resolve4git add resolved-file.js5git rebase --continueProblem: Accidentally Rebased Shared Branch
Solution:
1# Don't force push! Instead:2git reflog # Find commit before rebase3git reset --hard HEAD@{N} # N = position in reflog4git push origin branch --force-with-leaseProblem: Lost Commits After Rebase
Solution:
1git reflog # Find lost commits2git checkout -b recovery-branch <commit-hash>3# Review and merge back if neededRebase vs Merge: Decision Guide
Use Rebase when:
Use Merge when:
Conclusion
Git rebase is a powerful tool for maintaining clean commit history. Start with simple rebases to update feature branches, then progress to interactive rebasing for commit cleanup. Remember:
1. Never rebase shared branches without team coordination
2. Always test after rebasing
3. Use reflog to recover from mistakes
4. Communicate with your team about rebase usage
5. Practice on feature branches first
Mastering rebase will make you a more efficient developer and help maintain professional Git histories. Start with the easy examples, practice on your feature branches, and gradually work up to advanced interactive rebasing. Your future self (and teammates) will thank you for clean, readable commit histories!
Enjoyed this article?
Support our work and help us create more free content for developers.
Stay Updated
Get the latest articles and updates delivered to your inbox.