Squashing Commits with Interactive Rebase
2. The Interactive Rebase Method
Interactive rebase is your go-to tool for reshaping your commit history. It gives you fine-grained control over which commits to merge, reorder, or even drop entirely. It sounds intimidating, but trust me, it's manageable with a little practice. Think of it like plastic surgery for your Git log; a little nip here, a tuck there, and you're looking good.
First, you'll need to start the interactive rebase process. Use this command, replacing `HEAD~3` with the number of commits you want to include (in this case, the last three): `git rebase -i HEAD~3`. This opens a text editor with a list of your last three commits. The order is reversed, with the oldest commit at the top.
In the editor, change the word `pick` to `squash` (or just `s`) for the second and third commits. Leave the first commit as `pick`. This tells Git to combine the second and third commits into the first one. Save the file and close the editor. Git will then open another editor window, prompting you to write a new commit message for the combined commit. You can either edit the message to be more descriptive or just accept the default, which concatenates the original messages.
If things go sideways (and they sometimes do!), don't panic. You can always use `git rebase --abort` to back out and start over. Interactive rebase can be a bit scary the first time, but it's a powerful tool to have in your Git arsenal.