Skip to content

Summary: Rebase, deeper

Interactive rebase replays a sequence of commits and lets you edit, combine, drop, or reorder them along the way, producing a new clean history in place of the original messy one.

Terminal window
git rebase -i HEAD~N # interactive rebase of last N commits
# editor opens with todo list (oldest first)
# edit actions, save, close
# git replays each commit per the actions
# resolve conflicts if they happen (--continue, --skip, --abort)
  • pick: replay as-is (default)
  • reword: replay, edit the message
  • squash: combine into previous, edit combined message
  • fixup: combine into previous, discard this message
  • drop: delete the commit
  • edit: pause after replay so you can amend content
  • exec: run a shell command (e.g. tests) at this step
  • break: pause for any reason
Terminal window
git commit --fixup=<sha> # creates "fixup! <original-message>"
# ... continue work ...
git rebase -i --autosquash main # pre-arranges fixup commits at cleanup

Don’t rebase published commits.

If you’ve pushed a commit to a shared branch and someone may have pulled it, do not rewrite it. Force-pushing rewritten history to main or any shared branch causes hours of recovery work and risks losing teammates’ commits.

Defenses:

  • Branch protection on main forbids force-push entirely (platform setting; turn it on)
  • git push --force-with-lease instead of --force on your own feature branches: checks that remote hasn’t advanced before overwriting
  • Triple-check your branch before any force-push
SituationMove
Mid-rebase, want to give upgit rebase --abort
Already finished, want to undogit reflog, then git reset --hard HEAD@{N}
Force-pushed wrong history to shared branchFind someone with old history locally, force-push back, teammates reset
Stuck in a conflict storm--abort, squash locally first (fewer conflicts), try again

Rebase when: local cleanup, linear history convention, integrating into a branch nobody else pulls from.

Merge when: branch is published and others pulled, want to preserve parallel-development story, long-lived release branches with audit-trail value.

Git stores snapshots. Every other command is just navigating those snapshots.

Rebase replays snapshots onto a new base, creating new snapshots. Interactive rebase lets you decide which to keep, combine, or drop. The reflog records every HEAD state for 90 days.

Phase 4 (L13-L16) crosses into multi-agent territory: worktrees (L13), integration patterns for parallel agents (L14), AI-authored commits and PRs (L15), and the future of git in an AI-collaborative world (L16).