git rebase Command Guide
Reapply commits on top of another base tip
Dai Aoki
CEO at init, Inc. / CTO at US & JP startups / Creator of WebTerm
Quick Reference
Basic
git rebase <branch>Rebase onto branchgit rebase -i HEAD~3Interactive last 3git rebase --ontoRebase onto targetInteractive
pickUse commitsquashCombine with previousrewordEdit messageControl
--continueContinue after fix--abortCancel rebase--skipSkip current commitDownloadable Image 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.
# 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 mergeInteractive Rebase
Interactive rebase (-i) lets you edit, reorder, squash, or drop commits:
# 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 closeInteractive Commands
| Command | Short | Description |
|---|---|---|
| pick | p | Use commit as-is |
| reword | r | Use commit, edit message |
| edit | e | Stop for amending |
| squash | s | Combine with previous commit |
| fixup | f | Like squash, discard message |
| drop | d | Remove commit |
# 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 commitHandling Conflicts
# 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 --abortRebase 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
# 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 branchgit rebase -i HEAD~n- Interactive rebasegit rebase --continue- Continue after conflictgit rebase --abort- Cancel rebase
Official Documentation
For authoritative information, refer to the official documentation: