When to Rewrite History vs. Create New Commits
You'll learn: When it's safe to rewrite Git history and when you should preserve it by creating new commits instead.
The Core Dilemma
You've learned powerful tools like git commit --amend, interactive rebase, and cherry-picking that can rewrite history—changing commits after they're created. But with great power comes great responsibility. The key question: Should you rewrite history to make it cleaner, or preserve it exactly as it happened?
The Golden Rule: Never Rewrite Shared History
If others have pulled your commits, don't rewrite them. Rewriting shared history creates conflicting versions of the project's past, forcing teammates to untangle the mess. It's like editing chapters of a book after people have already read them—everyone gets confused about which version is real.
When to Rewrite History
Before pushing to shared branches, history rewriting is safe and valuable:
- Fix typos in commit messages with
git commit --amend - Combine messy work-in-progress commits into logical units with interactive rebase
- Remove accidentally committed secrets or files
- Create a clean, professional history for code reviews
Think of this as editing your draft before publication.
When to Preserve History
After pushing to shared branches, create new commits instead:
- Use
git revertto undo mistakes (creates a new "undo" commit) - Add fixes as new commits rather than amending
- Accept that history includes the actual journey, warts and all
This is especially critical on main or master branches where many developers collaborate.
The Reflog Exception
Remember: even if you rewrite history locally and regret it, the reflog is your safety net—you can recover the old commits.
Key Takeaway: Rewrite history freely on local or private branches for cleanliness, but never rewrite shared history—preserve it with new commits to keep your team sane and synchronized.