Git Rebase Cheatsheet
2.47Interactive 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
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.
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
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:
- Edit the conflicted files to resolve the conflicts
git addthe resolved filesgit rebase --continueto move on
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.