Skip
Arish's avatar

46. How to Rebase Changes


Rebasing in Git

Rebase is an alternative to merge that creates a linear history by replaying commits.

What is Rebase?

Rebase moves your branch's commits to start from a different point:

Before rebase: main: A ─ B ─ C ─ D \ feature: E ─ F After rebase: main: A ─ B ─ C ─ D \ feature: E' ─ F'

Your commits (E, F) are replayed as new commits (E', F') on top of main.

Basic Rebase

bash
1# Switch to feature branch
2git checkout feature-branch
3
4# Rebase onto main
5git rebase main

Rebase vs Merge

Merge

A ─ B ─ C ─────── M \ / D ─ E ────
  • Preserves history
  • Creates merge commit
  • Shows branch existed

Rebase

A ─ B ─ C ─ D' ─ E'
  • Linear history
  • No merge commits
  • Cleaner log

When to Use Rebase

✅ Good for Rebase

  • Updating feature branch with main
  • Cleaning up local commits
  • Before merging to main

❌ Avoid Rebase

  • On shared/public branches
  • After pushing commits
  • When history matters

Interactive Rebase

Edit, squash, or reorder commits:

bash
1git rebase -i HEAD~3  # Last 3 commits

Interactive Options

pick abc1234 First commit pick def5678 Second commit pick ghi9012 Third commit # Commands: # p, pick = use commit # r, reword = edit message # e, edit = stop for amending # s, squash = combine with previous # f, fixup = squash, discard message # d, drop = remove commit

Squash Commits

bash
1git rebase -i HEAD~3

Change to:

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

Result: Three commits become one.

Reword Commit Message

reword abc1234 First commit pick def5678 Second commit

Editor opens to change the message.

Rebase Workflow

Update Feature Branch

bash
1# On feature branch
2git checkout feature/my-feature
3
4# Fetch latest main
5git fetch origin main
6
7# Rebase onto main
8git rebase origin/main
9
10# If conflicts, resolve and continue
11git add .
12git rebase --continue
13
14# Force push (required after rebase)
15git push --force-with-lease origin feature/my-feature

Clean Up Before PR

bash
1# Squash WIP commits
2git rebase -i main
3
4# Make commit history clean
5# Then push
6git push --force-with-lease

Handling Rebase Conflicts

bash
1git rebase main
2# CONFLICT: Resolve in file.txt

Resolution Steps

bash
1# 1. Fix conflicts in the file
2# 2. Stage the fix
3git add file.txt
4
5# 3. Continue rebase
6git rebase --continue
7
8# 4. Or abort if needed
9git rebase --abort

The Golden Rule of Rebasing

Never rebase commits that have been pushed to a shared repository

Why? Rebase rewrites history. If others have based work on your commits, their history diverges.

Safe Rebase

bash
1# Your own feature branch (not shared)
2git checkout my-feature
3git rebase main
4git push --force-with-lease  # OK - your branch

Dangerous Rebase

bash
1# Never do this on shared branches!
2git checkout main
3git rebase feature  # ❌ Don't rebase main!

Force Push After Rebase

Rebase changes commit hashes, so you need force push:

bash
1# Regular force (dangerous)
2git push --force
3
4# Safer: fails if remote has new commits
5git push --force-with-lease

Always use --force-with-lease to avoid overwriting others' work.

Rebase Best Practices

1. Rebase Feature Branches Regularly

bash
1# Keep feature branch updated
2git fetch origin
3git rebase origin/main

2. Squash Before Merging

bash
1# Clean up WIP commits
2git rebase -i main
3# Squash related commits together

3. Don't Rebase Public History

If commits are shared, use merge instead.

4. Use Meaningful Commit Messages

After squashing, write a good combined message.

Summary

CommandDescription
git rebase mainRebase onto main
git rebase -i HEAD~nInteractive rebase
git rebase --continueContinue after conflict
git rebase --abortCancel rebase
git push --force-with-leaseSafe force push

Rebase creates cleaner history but requires care with shared branches!