← Back to Chapters

Git Rebase

? Git Rebase

? Quick Overview

Git rebase is a powerful command used to integrate changes from one branch into another by rewriting commit history into a clean, linear sequence.

? Key Concepts

  • Linear commit history
  • No merge commits
  • History rewriting
  • Conflict resolution during replay

? Syntax / Theory

? View Code Example
// Rebase current branch onto another branch
git rebase main

? Code Examples

? View Code Example
// Switch to feature branch
git checkout feature-branch
? View Code Example
// Fetch latest changes from remote
git fetch origin
? View Code Example
// Rebase feature branch onto main
git rebase origin/main

? Live Output / Explanation

The commits from the feature branch are replayed one by one on top of the main branch, resulting in a clean history.

? Interactive Rebase

? View Code Example
// Start interactive rebase for last 3 commits
git rebase -i HEAD~3

⚖️ Rebase vs Merge

  • Rebase rewrites history
  • Merge preserves branch context
  • Rebase creates linear history

? Use Cases

  • Cleaning up commit history
  • Updating feature branches
  • Preparing branches before merge

? Tips & Best Practices

  • Rebase before merging
  • Use interactive rebase for cleanup
  • Avoid rebasing shared branches

? Try It Yourself

  • Rebase a feature branch onto main
  • Squash commits using interactive rebase
  • Resolve conflicts during rebase