Why rebase
Rebase rewrites your branch so it appears to start from the tip of another branch, replaying your commits one by one. The result is a linear history that reads like a story, instead of a railway map of merge commits. For long-running feature branches, periodic rebases keep the diff small and the conflicts manageable.
The basic rebase
git checkout feature/login
git fetch origin
git rebase origin/main
If conflicts arise, Git pauses on each commit. Resolve, then continue:
git status # see the conflict
# edit the conflicted files
git add <file>
git rebase --continue
Abort at any time with git rebase --abort to return to the pre-rebase state.
Interactive rebase
The real power is interactive rebase. Pass a base commit and Git opens an editor with a script you can edit:
git rebase -i HEAD~5
You will see something like:
pick 1a2b3c4 Add login form
pick 5d6e7f8 Fix typo
pick 9a0b1c2 Refactor auth helper
pick 3d4e5f6 Add tests
pick 7a8b9c0 Address review feedback
Change pick to:
reword- keep the commit, edit the message.edit- pause to amend the commit.squash- combine into the previous commit and merge messages.fixup- combine into the previous commit, discard message.drop- remove the commit entirely.- Reorder lines to reorder commits.
The Golden Rule
Never rebase commits that have been pushed and consumed by others. Rebase rewrites SHAs; collaborators will end up with two copies of every commit and chaos at merge time. The exception is your own feature branch in your own fork - rebase it freely.
Autosquash workflow
Mark fixups during development:
git commit --fixup <sha>
git commit --fixup <sha>
# When ready to clean up:
git rebase -i --autosquash main
Git automatically reorders and squashes fixup commits into their targets.
Rebase versus merge
Both integrate changes; the philosophical difference is whether you preserve the actual development history (merge) or present a curated narrative (rebase). Many teams use merge for the long-lived main branch and rebase within feature branches before merging.
Rerere for repeated conflicts
git config --global rerere.enabled true
"Reuse recorded resolution" remembers how you resolved a conflict and applies the same resolution next time the same conflict appears. Indispensable on long-running feature branches.
Master rebase and your history becomes a tool, not a record.