Rebase vs Merge

Rebase vs Merge

Rebase vs Merge

Introduction

Rebase and merge are two ways to integrate changes from one branch to another. Understanding when to use each is crucial.

Merge

Creates a merge commit that combines histories:

main:     A---B---M
             \   /
feature:  C---D---E

Pros:

  • Preserves complete history
  • Shows when branches diverged
  • Safe, non-destructive
Cons:
  • Creates merge commits
  • History can become complex
  • More commits in history

Rebase

Replays commits on top of another branch:

Before rebase:
main:     A---B
             \
feature:  C---D---E

After rebase: main: A---B---C'--D'--E'

Pros:

  • Linear, clean history
  • Easier to follow
  • No merge commits
Cons:
  • Rewrites commit history
  • Dangerous on shared branches
  • Can lose context

When to Use Merge

  • Working on public/shared branches
  • Want to preserve complete history
  • Need to track when branches diverged
  • Team workflow requires merge commits

When to Use Rebase

  • Local feature branches
  • Before submitting pull request
  • Want clean, linear history
  • Updating feature branch with main changes

Basic Rebase Commands

Rebase Feature onto Main

git checkout feature-branch
git rebase main

Equivalent to:

git rebase main feature-branch

Continue After Resolution

If conflicts occur:

# Fix conflicts
git add .
git rebase --continue

Skip current commit

git rebase --skip

Abort rebase

git rebase --abort

Interactive Rebase

Squash Commits

git rebase -i HEAD~3

This opens an editor:

pick abc1234 First commit
pick def5678 Second commit
pick ghi9012 Third commit

Change to:

pick abc1234 First commit
squash def5678 Second commit
squash ghi9012 Third commit

This combines commits into one.

Edit Commits

pick abc1234 Add feature
reword def5678 Fix typo

Reorder Commits

Simply reorder lines in the editor.

Rebase vs Merge Examples

Merge Example

git checkout main
git merge feature-login

History becomes:

*   Merge branch 'feature-login'
|\
| * Add login feature
* | Fix bug
|/
* Initial commit

Rebase Example

git checkout feature-login
git rebase main
git checkout main
git merge feature-login

History becomes:

* Add login feature
* Fix bug
* Initial commit

Golden Rules

NEVER Rebase Public Branches

Never rebase commits that have been pushed to shared repository.

# BAD - will cause problems
git push --force origin main

GOOD - rebase local only

git rebase main git push

Use Merge for Team Branches

  • Main branch
  • Shared feature branches
  • Release branches

Use Rebase for Local Branches

  • Your personal feature branches
  • Before creating PR
  • Cleaning up local commits

Common Workflow

Feature Branch Workflow

# Start feature
git checkout -b feature-login

Make commits

git commit -m "Add login form" git commit -m "Add validation"

Update with latest main

git fetch origin git rebase origin/main

Force push (OK for feature branch not yet merged)

git push --force-with-lease

Pull Request Workflow

# Before creating PR
git checkout feature
git rebase main

Push

git push --force-with-lease

Create PR

Undoing Rebase

If Something Went Wrong

git reflog

Find the state before rebase:

abc1234 HEAD@{2}: rebase finished: ...
def5678 HEAD@{3}: checkout: moving to main

Recover:

git reset --hard def5678

Or:

git reset --hard ORIG_HEAD

Summary

AspectMergeRebase
HistoryPreserves branchingLinear
CommitsMerge commit createdCommits rewritten
SafetySafeRisky on shared
Use caseShared branchesLocal cleaning
Choose based on your workflow and team conventions.

Next Lesson

Learn about reset, revert, and restore commands.

Quiz - Quiz - Rebase vs Merge

1. What does git rebase do?

2. Why should you never rebase public/shared branches?

3. What is the difference between merge and rebase in terms of history?

4. What does 'git rebase --continue' do?

5. What is interactive rebase used for?

Resolving Merge Conflicts