Git Rebase Cheatsheet

2.47

Interactive rebase, squashing, and rewriting history with git

Official docs →

Rebasing is one of those git features that sounds scary until you understand what it actually does: it replays your commits on top of a different base. That's it. The interactive version lets you rewrite history before sharing it — squashing noisy commits, rewording messages, reordering changes. It's the difference between a messy commit log and a clean story.

The golden rule: never rebase commits that have been pushed and shared with others. Rebase is for cleaning up your local work before merging. Once you push, treat history as immutable.

Basic Rebase

Rebase Commands
git rebase main
Rebase current branch onto main
git rebase origin/main
Rebase onto remote main
git rebase --onto main feature old-feature
Transplant commits between branches
git rebase --continue
Continue after resolving conflicts
git rebase --abort
Cancel rebase and restore original state
git rebase --skip
Skip the current conflicting commit

If a rebase goes sideways, git rebase --abort always gets you back to where you started. There's no shame in aborting and trying again — even experienced developers do it regularly.

Interactive Rebase

Interactive rebase (git rebase -i) is where the real power is. It opens your editor with a list of commits and lets you decide what to do with each one.

Starting Interactive Rebase
git rebase -i HEAD~3
Interactively rebase last 3 commits
git rebase -i main
Interactively rebase all commits since main
git rebase -i --root
Rebase all commits from the very beginning
Interactive Rebase Actions
pick
Keep the commit as-is (default)
reword
Keep the commit but edit the message
edit
Pause at this commit to amend it
squash
Merge into previous commit, combine messages
fixup
Merge into previous commit, discard this message
drop
Remove the commit entirely

fixup is usually what you want instead of squash. It merges the commit into the one above it and throws away the message — cleaner than squash which asks you to edit combined messages.

Common Workflows

Real-World Patterns
git rebase -i HEAD~5
Clean up your last 5 commits before opening a PR
git fetch && git rebase origin/main
Update your branch with latest main (instead of merge)
git commit --fixup=HEAD~2
Create a fixup commit targeting 2 commits ago
git rebase -i --autosquash HEAD~5
Auto-arrange fixup commits during interactive rebase

The --autosquash flag is a game-changer. Use git commit --fixup=<hash> to create fixup commits as you work, then git rebase -i --autosquash to automatically slot them in. Set git config --global rebase.autoSquash true to make it the default.

Handling Conflicts During Rebase

When a conflict occurs, git pauses the rebase and lets you resolve it:

  1. Edit the conflicted files to resolve the conflicts
  2. git add the resolved files
  3. git rebase --continue to move on
Conflict Resolution
git status
See which files have conflicts
git diff
View the conflicts in detail
git add .
Mark conflicts as resolved
git rebase --continue
Resume the rebase
git rebase --abort
Give up and go back to before the rebase

If you find yourself resolving the same conflicts over and over (e.g., rebasing regularly onto a fast-moving main branch), enable rerere: git config --global rerere.enabled true. Git will remember how you resolved conflicts and auto-apply the same resolution next time.

If you're new to rebasing, practice on a throwaway branch first. Create a branch, make some commits, rebase them interactively. Once you see how the commit list editor works, the mystery dissolves.

Related Tools