Start a rebase
Git opens a todo list with one line per commit, oldest first. Each line starts with a command; change the command to change what happens to that commit.
git switch feature
git rebase -i main
# or the last N commits
git rebase -i HEAD~5
The commands
| Command | Effect |
|---|---|
pick | Keep the commit as-is |
reword | Keep the changes, edit the message |
edit | Pause to amend the commit contents |
squash | Combine into the previous commit, edit the message |
fixup | Combine into the previous commit, keep the previous message |
drop | Remove the commit entirely |
exec | Run a shell command after that commit (tests, formatters) |
Reorder lines to reorder commits. If a commit depends on a later one, moving it earlier can produce conflicts.
Conflicts during rebase
Rebase replays commits one at a time, so conflicts are per-commit. Resolve, stage, and continue. --abort restores the original branch; --skip drops the current commit.
git status
git add <resolved-files>
git rebase --continue
# abort and return to the pre-rebase state
git rebase --abort
Rebase vs merge
| Aspect | Rebase | Merge |
|---|---|---|
| History | Linear | Preserves branch topology |
| Commit hashes | Rewritten | Unchanged |
| Safe on shared branches | No | Yes |
| Conflict resolution | Per commit, possibly repeated | Once, at the merge |
| Use for | Local cleanup before review | Integrating shared branches |
The force-push rule
After a rebase, the remote branch has diverged. Push with --force-with-lease, which refuses if the remote moved since your last fetch. Never use plain --force on a shared branch, and never rebase commits other people have based work on.
git push --force-with-lease origin feature
If a rebase goes wrong, git reflog still has the pre-rebase HEAD. git reset --hard ORIG_HEAD is the quick way back.