Merge with squash all changes from another branch as a single commit
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In the world of software development, version control is a critical aspect of maintaining sanity and order. Git, a distributed version control system, has become an indispensable tool for developers. A common task in Git is merging branches, which involves integrating changes from one branch into another. Among the various merging strategies in Git, "Merge with squash" is particularly useful when you want to incorporate changes from one branch into another as a single, cohesive commit. This approach simplifies the commit history by squashing all changes from a feature branch into a single commit on the main branch. This article will explore the details of performing a merge with squash in Git, along with examples and best practices.
What is a Merge with Squash?
A "Merge with squash" operation in Git involves combining all the changes from a feature branch into a single commit before merging it into the base branch. This is different from a regular merge, which keeps the commit history from the source branch intact.
The `git merge --squash` command is central to this operation. It takes all the changes made in a feature branch and squashes them into a new commit. This approach is beneficial when you want to keep the commit history clean and concise.
Why Use Merge with Squash?
- Clean History: A squashed merge results in a cleaner and more understandable commit history. Rather than having numerous small commits that may include "fix typo" or "debugging," the history shows a meaningful commit that represents a complete feature or bug fix.
- Atomic Commits: Squashed merges create atomic commits, encapsulating all related changes for a particular feature or bug fix. This atomicity improves the readability and maintainability of the repository.
- Simplified Undo: If a feature needs to be rolled back, a single commit can be easily reverted compared to rolling back multiple smaller commits.
- Reduced Merge Conflicts: By merging as a single commit, you may face fewer conflicts compared to merging a series of commits individually.
Performing a Merge with Squash
Example Scenario
Let's assume you have a feature branch `feature-branch` and a main branch `main`. You want to merge the changes from `feature-branch` into `main` as a single commit.
Step-by-Step Guide
- Checkout the Target BranchBegin by checking out the branch into which you want to merge the changes.

