Committing Refactorings Separately
You'll learn: Why keeping refactoring commits separate from feature commits makes code reviews easier and rollbacks safer.
The Problem with Mixed Commits
When you bundle refactorings (restructuring code) and new features into a single commit, reviewers face a tangled mess. They can't tell which lines changed because you renamed variables versus which lines implement actual new behavior. If a bug appears later, you can't easily roll back just the feature without losing the refactoring work.
Think of it like moving furniture while repainting a room. If you do both simultaneously and realize the new paint color looks terrible, you'd have to undo all the furniture rearrangement too—even though the layout was an improvement.
Keep Your Hats Separate
Remember the "two hats" principle: you're either adding functionality or restructuring existing code, never both at once. Your commits should reflect this separation:
- Refactoring commit: "Extract calculateDiscount method from checkout logic"
- Feature commit: "Add holiday discount calculation"
Benefits of Isolated Refactoring Commits
For code review: Reviewers can verify your refactoring commit made zero behavioral changes by checking that tests still pass. The feature commit then shows only new logic clearly.
For rollback: If your new feature causes production issues, you can revert just that commit while preserving the cleaner structure from your refactoring.
For debugging: When tracking down when a bug was introduced, isolated commits make it obvious whether the problem came from restructuring or new functionality.
Key Takeaway: Commit refactorings separately from feature changes so reviewers can focus on one type of change at a time and you can safely roll back features without losing structural improvements.