Featured

Git Rebase Complete Guide: Easy and Advanced Examples

T
Team
·15 min read
#git#github#rebase#version control#git workflow#developer tools

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:

  • Creates a merge commit
  • Preserves all commit history
  • Shows branch structure
  • History can be complex

  • Rebase:

  • No merge commit
  • Linear, clean history
  • Easier to read
  • Rewrites commit history

  • When to Use Rebase


    Use rebase when:

  • Working on feature branches
  • Want clean, linear history
  • Before merging to main
  • Squashing commits
  • Updating feature branch with main

  • Avoid rebase when:

  • Branch is shared/collaborated on
  • Commits are already pushed and others depend on them
  • Working on main/master branch directly

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


    bash
    1# Current situation:
    2# main: A---B---C---D
    3# feature: E---F
    4 
    5# Step 1: Switch to your feature branch
    6git checkout feature
    7 
    8# Step 2: Rebase onto main
    9git rebase main
    10 
    11# Result:
    12# main: A---B---C---D
    13# 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:

    bash
    1# You're working on a login feature
    2git checkout login-feature
    3 
    4# Main branch has security updates you need
    5git rebase main
    6 
    7# Your login commits are now on top of security updates

    Example 2: Simple Rebase with Conflict Resolution


    Scenario: Rebase encounters conflicts that need resolution.


    bash(29 lines, showing 15)
    1# Start rebase
    2git checkout feature
    3git rebase main
    4 
    5# If conflicts occur:
    6# Git will pause and show:
    7# CONFLICT (content): Merge conflict in app.js
    8 
    9# Step 1: Open conflicted file and resolve
    10# app.js will have conflict markers:
    11<<<<<<< HEAD
    12// Code from main branch
    13const apiUrl = 'https://api.example.com';
    14=======
    15// Code from your feature branch

    Aborting rebase:

    bash
    1# If you want to cancel the rebase
    2git rebase --abort
    3# Returns to state before rebase started

    Example 3: Rebasing Last N Commits


    Scenario: You want to rebase only the last 3 commits.


    bash
    1# Interactive rebase for last 3 commits
    2git rebase -i HEAD~3
    3 
    4# Opens editor with:
    5pick abc123 First commit
    6pick def456 Second commit
    7pick ghi789 Third commit
    8 
    9# You can now:
    10# - reword: Change commit message
    11# - edit: Modify commit
    12# - squash: Combine with previous
    13# - drop: Remove commit

    Advanced Rebase Examples


    Example 1: Interactive Rebase - Squashing Commits


    Scenario: You have multiple small commits and want to combine them into meaningful commits.


    bash(34 lines, showing 15)
    1# Current commits (messy):
    2# abc123 Add button
    3# def456 Fix typo
    4# ghi789 Style button
    5# jkl012 Add click handler
    6# mno345 Fix bug
    7 
    8# Start interactive rebase
    9git rebase -i HEAD~5
    10 
    11# Editor opens:
    12pick abc123 Add button
    13pick def456 Fix typo
    14pick ghi789 Style button
    15pick jkl012 Add click handler

    Interactive rebase commands:

  • `pick`: Use commit as-is
  • `reword`: Change commit message
  • `edit`: Modify commit (add/remove files)
  • `squash`: Combine with previous commit
  • `fixup`: Like squash but discard message
  • `drop`: Remove commit
  • `exec`: Run shell command

  • Example 2: Editing a Specific Commit


    Scenario: You need to add a file to a previous commit.


    bash(27 lines, showing 15)
    1# Start interactive rebase
    2git rebase -i HEAD~3
    3 
    4# Editor shows:
    5pick abc123 Add login form
    6pick def456 Add validation
    7pick ghi789 Add error handling
    8 
    9# Change to edit the first commit:
    10edit abc123 Add login form
    11pick def456 Add validation
    12pick ghi789 Add error handling
    13 
    14# Save and close. Git stops at that commit:
    15# Stopped at abc123... Add login form

    Example 3: Complex Rebase with Multiple Branches


    Scenario: You have a feature branch based on another feature branch, and both need to be rebased.


    bash(16 lines, showing 15)
    1# Initial state:
    2# main: A---B---C
    3# feature-1: D---E
    4# feature-2: F---G
    5 
    6# Step 1: Rebase feature-1 onto main
    7git checkout feature-1
    8git rebase main
    9# Result: feature-1 is now A---B---C---D'---E'
    10 
    11# Step 2: Rebase feature-2 onto feature-1
    12git checkout feature-2
    13git rebase feature-1
    14# 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.


    bash(19 lines, showing 15)
    1# You accidentally did: git rebase --abort (wrong branch)
    2# Lost your commits!
    3 
    4# Step 1: View reflog to find lost commits
    5git reflog
    6 
    7# Output shows:
    8# abc123 HEAD@{0}: rebase: abort
    9# def456 HEAD@{1}: commit: Your important work
    10# ghi789 HEAD@{2}: commit: Previous work
    11# jkl012 HEAD@{3}: checkout: moving from main to feature
    12 
    13# Step 2: Create branch from lost commit
    14git checkout -b recovered-feature def456
    15 

    Example 5: Rebase Onto Specific Commit


    Scenario: You want to rebase onto a specific commit, not a branch.


    bash
    1# Rebase current branch onto specific commit
    2git rebase --onto main abc123
    3 
    4# This means:
    5# - Take commits after abc123
    6# - Replay them on top of main
    7 
    8# Useful when you want to exclude some commits

    Advanced Interactive Rebase Workflow


    Complete Example: Cleaning Up Feature Branch


    bash(31 lines, showing 15)
    1# You have messy commits:
    2git log --oneline
    3# a1b2c3d Fix typo in comment
    4# e4f5g6h WIP: trying something
    5# h7i8j9k Actually, revert that
    6# j0k1l2m Add feature X
    7# k3l4m5n Fix bug in feature X
    8# l6m7n8o Add tests for feature X
    9# m9n0o1p Update documentation
    10 
    11# Step 1: Start interactive rebase
    12git rebase -i HEAD~7
    13 
    14# Editor opens. Organize commits:
    15pick j0k1l2m Add feature X

    Rebase Best Practices


    1. Always Rebase Before Pushing


    bash
    1# Good workflow:
    2git checkout feature
    3git rebase main # Update with latest main
    4git push origin feature
    5 
    6# Avoid:
    7git push origin feature
    8git rebase main # Don't rebase after pushing!
    9git push --force # Dangerous!

    2. Use --autostash for Uncommitted Changes


    bash
    1# If you have uncommitted changes:
    2git rebase --autostash main
    3 
    4# Git will:
    5# 1. Stash your changes
    6# 2. Perform rebase
    7# 3. Reapply your changes

    3. Test After Rebase


    bash
    1# Always test after rebase
    2git rebase main
    3npm test # Run your tests
    4# 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


    bash
    1# Regular workflow to keep feature branch fresh
    2git checkout main
    3git pull origin main
    4git checkout feature
    5git rebase main
    6git push origin feature --force-with-lease

    Scenario 2: Preparing for Pull Request


    bash
    1# Clean up before creating PR
    2git checkout feature
    3git rebase -i main # Interactive rebase
    4# Squash, reword, organize commits
    5git push origin feature --force-with-lease
    6# Create PR with clean history

    Scenario 3: Fixing Commit in Middle of History


    bash
    1# You need to fix commit 3 commits ago
    2git rebase -i HEAD~4
    3 
    4# Change:
    5pick abc123 Commit 1
    6pick def456 Commit 2
    7edit ghi789 Commit 3 (needs fix)
    8pick jkl012 Commit 4
    9 
    10# Git stops at commit 3
    11# Make your fixes
    12git add .
    13git commit --amend
    14git rebase --continue

    Troubleshooting Rebase


    Problem: Rebase Conflict


    Solution:

    bash
    1# Resolve conflicts in files
    2git status # See conflicted files
    3# Edit files to resolve
    4git add resolved-file.js
    5git rebase --continue

    Problem: Accidentally Rebased Shared Branch


    Solution:

    bash
    1# Don't force push! Instead:
    2git reflog # Find commit before rebase
    3git reset --hard HEAD@{N} # N = position in reflog
    4git push origin branch --force-with-lease

    Problem: Lost Commits After Rebase


    Solution:

    bash
    1git reflog # Find lost commits
    2git checkout -b recovery-branch <commit-hash>
    3# Review and merge back if needed

    Rebase vs Merge: Decision Guide


    Use Rebase when:

  • ✅ Feature branches
  • ✅ Personal branches
  • ✅ Want linear history
  • ✅ Before merging to main
  • ✅ Cleaning up commits

  • Use Merge when:

  • ✅ Main/master branch
  • ✅ Shared branches
  • ✅ Want to preserve branch structure
  • ✅ Public history (already pushed)
  • ✅ Team prefers merge workflow

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