Skip to content

Git Rebase

Terminal window
# Rebase current branch onto main
git rebase main
# Interactive rebase (last 3 commits)
git rebase -i HEAD~3
# Continue after resolving conflicts
git rebase --continue
# Abort rebase
git rebase --abort
# Force push after rebase (use with caution!)
git push --force-with-lease

Reapply commits on top of another base tip.

~ Git Documentation

Git rebase rewrites commit history by moving or combining a sequence of commits to a new base commit. This creates a cleaner, linear history compared to merge commits.

When you rebase, Git:

  1. Finds the common ancestor between your branch and the target branch
  2. Saves your commits temporarily
  3. Resets your branch to the target branch
  4. Reapplies your commits one by one on top
Before rebase:
A -- B -- C (main)
\
D -- E (feature)
After git rebase main:
A -- B -- C (main)
\
D' -- E' (feature)

The commits D' and E' have the same changes as D and E, but different commit hashes.

Terminal window
# Switch to your feature branch
git switch feature-branch
# Rebase onto main
git rebase main
# If there are conflicts, resolve them, then:
git add .
git rebase --continue
# Push the rebased branch (rewrites history!)
git push --force-with-lease

Interactive rebase lets you edit, reorder, squash, or delete commits:

Terminal window
# Rebase the last 5 commits interactively
git rebase -i HEAD~5
# Or rebase from a specific commit
git rebase -i abc1234

This opens an editor with your commits:

pick a1b2c3d Add user authentication
pick d4e5f6g Fix typo in login form
pick h7i8j9k Update tests
pick l0m1n2o Refactor validation logic
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard commit message
# d, drop = remove commit

Common operations:

# Squash last two commits together
pick a1b2c3d Add user authentication
pick d4e5f6g Fix typo in login form
squash h7i8j9k Update tests # Combines with previous
pick l0m1n2o Refactor validation logic
# Change commit message
pick a1b2c3d Add user authentication
reword d4e5f6g Fix typo in login form # Will prompt for new message
pick h7i8j9k Update tests
# Delete a commit
pick a1b2c3d Add user authentication
drop d4e5f6g Fix typo in login form # Removes this commit
pick h7i8j9k Update tests
Terminal window
# Clean up your feature branch before PR
git checkout feature-branch
git rebase -i main
# Squash "WIP" and "fix typo" commits
# Reword unclear commit messages
# Remove debug commits
Terminal window
# Get latest changes from main
git checkout main
git pull
# Update feature branch
git checkout feature-branch
git rebase main
Terminal window
# Change the last commit message
git commit --amend
# Change older commit messages
git rebase -i HEAD~5
# Change 'pick' to 'reword' for commits to edit
Terminal window
# Squash last 3 commits into one
git rebase -i HEAD~3
# Change 'pick' to 'squash' for commits 2 and 3

When conflicts occur during rebase:

Terminal window
# 1. Git stops and shows conflicting files
# 2. Edit files to resolve conflicts
# 3. Stage resolved files
git add .
# 4. Continue the rebase
git rebase --continue
# Or skip the commit causing issues
git rebase --skip
# Or abort and start over
git rebase --abort
Terminal window
# Abort rebase and return to original state
git rebase --abort
# Skip current commit during rebase
git rebase --skip
# Continue after resolving conflicts
git rebase --continue
# Edit a specific commit
git rebase -i HEAD~3
# Change 'pick' to 'edit' for the commit
# Rebase onto a different branch
git rebase --onto main feature-branch
FeatureRebaseMerge
HistoryLinear, cleanShows true history with merge commits
CommitsRewrites commit hashesPreserves original commits
ConflictsResolve per commitResolve once
Use forLocal cleanup, updating branchesIntegrating completed features
Team use⚠️ Never on public/shared branches✅ Safe for shared branches

Never rebase commits that have been pushed to a shared/public repository.

~ Every Git tutorial ever

Rebasing rewrites history. If others have based work on commits you rebase, you’ll cause serious problems.

Safe to rebase:

  • Local commits not yet pushed
  • Feature branches only you’re working on
  • After coordinating with your team

Never rebase:

  • The main or master branch
  • Commits others have based their work on
  • Public release tags
  • Use --force-with-lease instead of --force: It’s safer because it checks if someone else pushed changes
  • Rebase often: Keep your feature branch up-to-date with main
  • Communicate: Tell your team when you rebase shared branches
  • Keep backups: Create a backup branch before complex rebases: git branch backup-branch
  • Start simple: Practice on local branches before rebasing pushed work

Official Git documentation on git rebase

Atlassian tutorial on merging vs rebasing

Git SCM book chapter on rebasing

GitHub guide on interactive rebase

Julia Evans’ zine on git rebase