Git Rebase vs Merge

Understanding version control, one commit at a time.

Introduction

When working with Git, one of the most common tasks is integrating changes from one branch into another. Git provides two primary methods for accomplishing this: merge and rebase. While both achieve the goal of combining code from different branches, they do so in fundamentally different ways, each with its own advantages and use cases.

What is Git Merge?

Git merge is a non-destructive operation that combines two branches by creating a new "merge commit." This commit has two parent commits, preserving the complete history of both branches. When you merge a feature branch into your main branch, Git creates a new commit that ties together the histories of both branches.

Basic Merge Syntax

git checkout main
git merge feature-branch
Before merge: A---B---C (main) \ D---E (feature) After merge: A---B---C---F (main) \ / D---E (feature)

✓ Pros of Merge

  • Preserves complete history and context
  • Non-destructive operation
  • Easy to understand and use
  • Shows when branches were integrated
  • Safer for collaborative work

✗ Cons of Merge

  • Creates additional merge commits
  • Can result in cluttered history
  • Makes git log harder to read
  • Pollutes feature branch history

What is Git Rebase?

Git rebase is a process of moving or combining a sequence of commits to a new base commit. Rather than creating a merge commit, rebase rewrites the project history by creating brand new commits for each commit in the original branch. This results in a linear, cleaner project history.

Basic Rebase Syntax

git checkout feature-branch
git rebase main
Before rebase: A---B---C (main) \ D---E (feature) After rebase: A---B---C (main) \ D'---E' (feature)

✓ Pros of Rebase

  • Creates a linear project history
  • Cleaner, easier to read git log
  • Eliminates unnecessary merge commits
  • Makes it easier to navigate using git log, git bisect

✗ Cons of Rebase

  • Rewrites commit history
  • Can be dangerous on public branches
  • Loses context of when changes were merged
  • More complex to understand initially
  • Can create conflicts for each rebased commit

Key Differences

Aspect Merge Rebase
History Preserves original history with merge commits Creates linear history by rewriting commits
Commits Adds merge commit with two parents Replays commits on new base
Safety Safe for public branches Risky on shared branches
Readability Can become cluttered with many merges Clean, linear history
Traceability Easy to see when branches were merged Harder to trace integration points
Conflicts Resolve once during merge May resolve multiple times per commit

When to Use Merge

When to Use Rebase

Golden Rule of Rebasing: Never rebase commits that exist outside your local repository and that people may have based work on. This can cause serious issues for collaborators.

Interactive Rebase

One powerful feature of rebase is interactive mode, which allows you to modify commits during the rebase process. This is extremely useful for cleaning up your commit history before merging into a main branch.

git rebase -i HEAD~3

Interactive rebase allows you to:

Best Practices

For Merge

For Rebase

Common Workflows

Feature Branch Workflow with Rebase

# Create and work on feature branch
git checkout -b feature-branch
# ... make commits ...

# Update feature branch with latest main
git checkout main
git pull
git checkout feature-branch
git rebase main

# Clean up commits interactively
git rebase -i main

# Merge into main (creates merge commit)
git checkout main
git merge feature-branch

Simple Merge Workflow

# Create and work on feature branch
git checkout -b feature-branch
# ... make commits ...

# Update main and merge
git checkout main
git pull
git merge feature-branch
git push