bashintermediate

Interactive Rebase — Squash and Reorder

Use git rebase -i to squash, edit, reorder, or drop commits before pushing to clean up history.

bash
# Rebase last 5 commits interactively
git rebase -i HEAD~5

# In the editor:
# pick   abc1234  feat: add login      ← keep as-is
# squash abc1235  fix: login typo      ← squash into previous
# reword abc1236  feat: add logout     ← edit commit message
# edit   abc1237  feat: add profile    ← stop to amend
# drop   abc1238  wip: debug stuff     ← remove entirely

# After saving, Git replays commits with your changes.
# If you chose 'edit', make changes then:
git add .
git rebase --continue

# Abort if things go wrong:
git rebase --abort

# Force push after rebase (safe version):
git push --force-with-lease origin feature/my-branch

Sponsored

GitHub Copilot

Use Cases

  • Cleaning commit history before merging a PR
  • Squashing WIP commits into logical units
  • Reordering commits for a cleaner changelog

Tags

Related Snippets

Similar patterns you can reuse in the same workflow.