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.

bash
git switch feature
git rebase -i main
# or the last N commits
git rebase -i HEAD~5

The commands

CommandEffect
pickKeep the commit as-is
rewordKeep the changes, edit the message
editPause to amend the commit contents
squashCombine into the previous commit, edit the message
fixupCombine into the previous commit, keep the previous message
dropRemove the commit entirely
execRun 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.

bash
git status
git add <resolved-files>
git rebase --continue

# abort and return to the pre-rebase state
git rebase --abort

Rebase vs merge

AspectRebaseMerge
HistoryLinearPreserves branch topology
Commit hashesRewrittenUnchanged
Safe on shared branchesNoYes
Conflict resolutionPer commit, possibly repeatedOnce, at the merge
Use forLocal cleanup before reviewIntegrating 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.

bash
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.