What is Git Squash?
Git squash is a useful feature in version control that lets you combine multiple commits into a single, cleaner commit before merging them into a branch, such as master. It’s especially handy for keeping the commit history tidy and focused.

Let’s Understand with an Example
Imagine we have two branches in our repository:
- Master branch: Contains commits:
Commit 1,Commit 2,Commit 3. - Dev branch: Contains commits:
Commit 4,Commit 5,Commit 6.
Now, suppose we only want to merge the changes from Commit 5 and Commit 6 into the master branch as a single commit, without including Commit 4.
This is where the git squash command comes in!
Steps to Squash Commits
Follow these simple steps:
- Switch to the
masterbranch
git checkout master
2. Squash the commits from the dev branch
Use the following command to combine the latest two commits (Commit 5 and Commit 6) from the dev branch into a single commit and merge it into master:
git merge dev --squash HEAD~2--squash: Combines the specified commits into one.HEAD~2: Refers to the last two commits in thedevbranch (in this case,Commit 5andCommit 6).
Here is the YouTube Link: https://youtube.com/@raeesq.?si=v_QK6Q2XXMf9mKep
Why Use Git Squash?
Using Git squash ensures your commit history remains clean and concise. This is particularly helpful when working on a feature branch with several small, iterative commits. Instead of merging all these commits into the main branch, you can consolidate them into a single meaningful commit.
Try It Out!
Give it a shot, and you’ll see the magic of Git squash in action. It keeps your repository organized and makes it easier for your team to track changes.
Happy coding! 🎉
Comments
Post a Comment