Terminal GuideTerminal Guide

git rebase Command Guide

Reapply commits on top of another base tip

10 min readLast updated: January 19, 2026
Dai Aoki

Dai Aoki

CEO at init, Inc. / CTO at US & JP startups / Creator of WebTerm

Quick Reference

Basic

git rebase <branch>Rebase onto branch
git rebase -i HEAD~3Interactive last 3
git rebase --ontoRebase onto target

Interactive

pickUse commit
squashCombine with previous
rewordEdit message

Control

--continueContinue after fix
--abortCancel rebase
--skipSkip current commit

Downloadable Image Preview

Failed to generate preview

Basic Usage

The git rebase command moves commits to a new base. It replays your commits on top of another branch, creating a linear history.

bash
# Rebase current branch onto main
git checkout feature
git rebase main

# Rebase and then merge (common workflow)
git checkout feature
git rebase main
git checkout main
git merge feature  # Fast-forward merge
Warning
Never rebase commits that have been pushed to a shared branch. Rebasing rewrites history and will cause problems for others.

Interactive Rebase

Interactive rebase (-i) lets you edit, reorder, squash, or drop commits:

bash
# Interactive rebase last 3 commits
git rebase -i HEAD~3

# Interactive rebase from a commit
git rebase -i abc1234

# The editor shows:
pick abc1234 First commit message
pick def5678 Second commit message
pick ghi9012 Third commit message

# Edit the commands, save, and close

Interactive Commands

CommandShortDescription
pickpUse commit as-is
rewordrUse commit, edit message
editeStop for amending
squashsCombine with previous commit
fixupfLike squash, discard message
dropdRemove commit
bash
# Squash last 3 commits into one
git rebase -i HEAD~3
# Change to:
# pick abc1234 First commit
# squash def5678 Second commit
# squash ghi9012 Third commit

# Reorder commits (just change the order in editor)
# pick ghi9012 Third commit
# pick abc1234 First commit
# pick def5678 Second commit

# Edit a commit message
# reword abc1234 First commit

Handling Conflicts

bash
# When conflicts occur during rebase
git rebase main
# CONFLICT: Merge conflict in file.js

# Fix conflicts in the file, then:
git add file.js
git rebase --continue

# Skip this commit (lose its changes)
git rebase --skip

# Abort and return to original state
git rebase --abort

Rebase vs Merge

When to Use Each

Use Rebase

  • • Local/feature branches
  • • Before merging to main
  • • Cleaning up commits
  • • Linear history preference

Use Merge

  • • Shared/public branches
  • • Preserving exact history
  • • Team collaboration
  • • Simple conflict resolution

Practical Examples

bash
# Update feature branch with main
git checkout feature
git rebase main
git push --force-with-lease

# Squash all commits before PR
git rebase -i main
# Mark all but first as 'squash'

# Split a commit (using edit)
git rebase -i HEAD~3
# Change 'pick' to 'edit' for target commit
git reset HEAD~1
git add -p && git commit
git add -p && git commit
git rebase --continue

# Undo a rebase
git reflog
git reset --hard HEAD@{2}

Frequently Asked Questions

When should I use rebase instead of merge?

Use rebase for local/feature branches to maintain clean linear history. Use merge for shared branches or when you want to preserve the exact history of how development happened.

Why is rebasing pushed commits dangerous?

Rebasing rewrites commit history (creates new commit SHAs). If others have pulled the original commits, their history diverges from yours, causing confusion and potential data loss.

How do I squash commits?

Use "git rebase -i HEAD~n" where n is the number of commits. In the editor, change "pick" to "squash" (or "s") for commits you want to combine into the previous one.

How do I undo a rebase?

Use "git reflog" to find the commit before the rebase, then "git reset --hard HEAD@{n}" where n is the reflog entry number. This works if you haven't pushed yet.

What is git rebase --onto?

It lets you move a series of commits from one base to another. Useful when you branched from the wrong place or need to move commits between branches.

Summary

git rebase creates clean, linear history by replaying commits. Use interactive rebase to squash, reorder, and edit commits before sharing.

Quick Reference

  • git rebase <branch> - Rebase onto branch
  • git rebase -i HEAD~n - Interactive rebase
  • git rebase --continue - Continue after conflict
  • git rebase --abort - Cancel rebase

Official Documentation

For authoritative information, refer to the official documentation:

Related Articles