Welcome to our guide on squashing commits! In this lesson, we'll learn what squashing commits means, why it's useful, and how to do it using Git. 📝
In Git, a commit is a snapshot of your project's current state. Each commit contains changes, a commit message, and a unique identifier. 💡 Commits help you track your project's history and changes.
Squashing commits combines multiple commits into one, making the commit history cleaner and easier to understand. This is particularly useful when you have many small commits for a single feature or bug fix.
Let's squash commits using the interactive rebase feature of Git.
git checkout <your-branch>git rebase -i HEAD~<number-of-commits>Replace <number-of-commits> with the number of commits you want to squash.
pick <commit-hash> <commit-message>pick to squash on the lines of the commits you want to combine. The result will look like this:squash <commit-hash-1> <commit-message-1>
squash <commit-hash-2> <commit-message-2>Save and close the editor. Git will prompt you to enter a new commit message for the combined commits.
Finally, force push the branch to update the commit history:
git push --force-with-leaseAnd that's it! You've squashed commits using Git. ✅
Let's squash the last three commits in our feature/my-feature branch:
git checkout feature/my-feature
git rebase -i HEAD~3Change pick to squash on the lines of the commits you want to combine, save and close the editor, enter a new commit message, and force push the branch.
What does squashing commits do?
Happy coding! 😊