snippetbashgitModeratepending
Git interactive rebase for clean history
Viewed 0 times
interactive rebasesquashfixupclean historyautosquash
Problem
Feature branch has messy commits (WIP, fixup, typo fixes) that need to be cleaned before merging.
Solution
Clean up commits with interactive rebase:
Safe workflow:
# Rebase last N commits
git rebase -i HEAD~5
# Rebase from branch point
git rebase -i main
# Editor opens with commit list:
# pick abc1234 Add user model
# pick def5678 WIP
# pick ghi9012 Fix typo
# pick jkl3456 Add user API endpoint
# pick mno7890 Fix tests
# Change to:
# pick abc1234 Add user model
# squash def5678 WIP <- merge into previous
# fixup ghi9012 Fix typo <- merge, discard message
# pick jkl3456 Add user API endpoint
# squash mno7890 Fix tests <- merge into previous
# Commands:
# pick = keep commit as-is
# squash = merge into previous, combine messages
# fixup = merge into previous, discard this message
# reword = keep commit, edit message
# edit = pause to amend commit
# drop = remove commit entirelySafe workflow:
# 1. Create backup branch
git branch backup-feature
# 2. Rebase
git rebase -i main
# 3. If something goes wrong
git rebase --abort
# Or restore from backup
git reset --hard backup-feature
# 4. Auto-squash fixup commits
git commit --fixup=abc1234 # Creates: fixup! Add user model
git rebase -i --autosquash main # Automatically reordersWhy
Clean commit history makes code review easier, git bisect more useful, and the project history more navigable.
Gotchas
- Never rebase commits that have been pushed to a shared branch
- Rebase rewrites commit hashes - force push required after
Context
Cleaning up feature branch commits before merge/PR
Revisions (0)
No revisions yet.