When working on a project, you might end up with several small commits that clutter your history.
Squashing multiple commits into one keeps your Git log clean and makes collaboration easier.
In this step-by-step guide, let’s find out how to squash commits with a simple demo so you can streamline your commit history.
Steps to follow:
1. View your commit history to identify the commits you want to squash by running 'git log --oneline'.
2. Start an interactive rebase for the last few commits by replacing 'N' with the number of commits you want to squash with 'git rebase -i HEAD~N'.
3. In the text editor that opens, you’ll see a list of commits. Change 'pick' to 'squash' (or simply 's') for all the commits you want to squash into the first one. Leave the first commit as 'pick'.
4. After saving, another editor will open where you can edit the commit message for the squashed commit. Modify the message as needed, then save and close the editor.
5. Once the rebase is complete, the process is successful.
6. Check your commit history to ensure the commits have been squashed by running 'git log --oneline'.
That's it!
You’ve successfully squashed multiple commits into one.
By squashing multiple commits, you make your codebase more organized and easier to review.
Now that you know how to do it, you can keep your history tidy and avoid unnecessary commit clutter in your projects.
Still have questions?
Send an email to archana@leadwalnut.com, OR
Book a FREE consultation with an expert developer here.