In this lesson, you will learn how to transform a messy trail of "work-in-progress" commits into a single, professional unit of work using interactive rebasing.
As you develop a feature, you likely save your progress frequently with messages like "fixed typo," "added logic," or "oops, forgot a semicolon." While these granular commits are great for local safety, they clutter the project history once the feature is ready for the main branch. A clean timeline should represent logical milestones, not every keystroke.
The Strategy: Squashing
"Squashing" is the process of collapsing a series of commits into a single, cohesive entry. This is achieved via an interactive rebase.
When you run git rebase -i HEAD~n (where n is the number of commits you want to clean up), Git opens an editor listing your recent history. By changing the command from pick to squash (or just s) for the commits you want to fold into the one above them, you instruct Git to merge those changes into the base commit.
pick a1b2c3d Added core feature logic
squash e4f5g6h Fixed minor typo
squash i7j8k9l Finalized unit tests
Once you save and close, Git prompts you to combine the commit messages. You can then rewrite the history into one clear, descriptive summary: "Implement core feature logic and associated unit tests."
Why It Matters
A clean timeline acts as documentation for future maintainers. When a project lead looks at your history, they want to see the "what" and "why" of a change, not the "how" of your trial-and-error process. By squashing, you ensure that your contribution appears as a deliberate, thoughtful addition to the codebase rather than a chaotic log of your personal development session.
Remember, always squash your commits before merging your feature branch into the main project history. This keeps the primary branch pristine and makes features much easier to revert or audit if bugs arise later.
Key Takeaway: Use interactive rebasing to squash granular work-in-progress commits into a single, meaningful unit of work, ensuring your project history remains professional, readable, and focused on logical milestones.