snippetgitModeratepending
Git interactive rebase for cleaning up commit history
Viewed 0 times
rebaseinteractivesquashfixuprewordhistory
Problem
Feature branch has messy commits ('WIP', 'fix typo', 'actually fix it') that should be cleaned up before merging.
Solution
Use interactive rebase to rewrite history (LOCAL ONLY!):
# Rebase last N commits:
git rebase -i HEAD~5
# Or rebase onto a branch point:
git rebase -i main
# Editor opens with commit list:
# pick abc1234 Add user authentication
# pick def5678 WIP
# pick ghi9012 Fix typo in auth
# pick jkl3456 Actually fix the bug
# pick mno7890 Add tests
# Change to:
# pick abc1234 Add user authentication
# squash def5678 WIP
# squash ghi9012 Fix typo in auth
# squash jkl3456 Actually fix the bug
# pick mno7890 Add tests
# Commands:
# pick = keep commit as-is
# squash = merge into previous commit (combine messages)
# fixup = merge into previous commit (discard message)
# reword = keep commit but change message
# edit = pause to amend commit
# drop = delete commit entirely
# After saving, force push YOUR branch:
git push --force-with-lease origin feature-branch
# NEVER rebase commits that others have pulled!
# Only rebase YOUR unpushed or personal branch commits
# Abort if something goes wrong:
git rebase --abort
# Rebase last N commits:
git rebase -i HEAD~5
# Or rebase onto a branch point:
git rebase -i main
# Editor opens with commit list:
# pick abc1234 Add user authentication
# pick def5678 WIP
# pick ghi9012 Fix typo in auth
# pick jkl3456 Actually fix the bug
# pick mno7890 Add tests
# Change to:
# pick abc1234 Add user authentication
# squash def5678 WIP
# squash ghi9012 Fix typo in auth
# squash jkl3456 Actually fix the bug
# pick mno7890 Add tests
# Commands:
# pick = keep commit as-is
# squash = merge into previous commit (combine messages)
# fixup = merge into previous commit (discard message)
# reword = keep commit but change message
# edit = pause to amend commit
# drop = delete commit entirely
# After saving, force push YOUR branch:
git push --force-with-lease origin feature-branch
# NEVER rebase commits that others have pulled!
# Only rebase YOUR unpushed or personal branch commits
# Abort if something goes wrong:
git rebase --abort
Why
Clean commit history makes code review easier and git log useful. Each commit should represent one logical change.
Revisions (0)
No revisions yet.