Skip to content

Git Merge vs Rebase — Which One Should You Use?

git merge and git rebase both integrate changes from one branch into another — but they work differently.

The Difference

Merge creates a new commit that ties two branch histories together.

Rebase rewrites history by replaying your commits on top of another branch.

# Merge preserves history
    A---B---C main
         \
          D---E feature
                  \
    A---B---C------F main (merge commit)

# Rebase rewrites history
    A---B---C main
                \
                 D'---E' feature (new commits)

When to Merge

Merging to main (shared branch)

git checkout main
git merge feature

Use merge when integrating a feature branch into a shared branch like main or develop. The merge commit makes it clear when the feature was integrated.

Public branches

Never rebase branches that others are working on. Use merge for any branch that has been pushed and shared.

When to Rebase

Cleaning up local history

git checkout feature
git rebase main

Before pushing a feature branch, rebase it onto the latest main. This keeps history linear and avoids unnecessary merge commits.

Interactive rebase

git rebase -i HEAD~5

Squash, reorder, and reword commits before pushing. Never do this after pushing.

The Golden Rule

Never rebase a branch that others have based work on.

If a branch has been pushed and shared, use merge. Rebasing a shared branch creates duplicate commits and breaks everyone else’s history.

Example Workflow

# Start a feature
git checkout -b my-feature main

# Work, commit, work, commit
git add .
git commit -m "Add feature part 1"
git add .
git commit -m "Add feature part 2"

# Update with latest main (rebase locally)
git rebase main

# Clean up commits
git rebase -i HEAD~2

# Push (first time)
git push -u origin my-feature

# Open PR, get review, make changes
git add .
git commit -m "Fix review feedback"
git rebase -i HEAD~3  # squash fixup into proper commits

# Force push (safe here — it's your feature branch)
git push --force-with-lease

# Merge PR (use merge, not rebase, on main)
# GitHub: "Create a merge commit"

Resolving Conflicts

During merge

git merge feature
# CONFLICT in file.txt
# Fix conflicts
git add file.txt
git merge --continue

During rebase

git rebase main
# CONFLICT in file.txt
# Fix conflicts
git add file.txt
git rebase --continue

# Or skip this commit
git rebase --skip

# Or abort entirely
git rebase --abort

PR Workflow Comparison

ActionMergeRebase
Update feature branchgit merge maingit rebase main
History on mainLinear + merge commitsLinear only
Commit authorshipPreservedPreserved (new hashes)
Safe for shared branchesYesNo

Quick Decision Guide

SituationUse
Integrating a PR into mainMerge
Updating your local feature branchRebase
Shared branch with collaboratorsMerge
Cleaning commits before pushRebase (interactive)
Branch that has been pushedMerge
Branch you haven’t pushed yetRebase

Related: Learn Git branching strategies and advanced Git commands.