How to Merge and Squash in Git
- Understanding Merging in Git
- Squashing Commits in Git
- Best Practices for Merging and Squashing
- Conclusion
- FAQ

Git is an essential tool for developers, enabling efficient version control and collaboration. Understanding how to merge and squash commits is crucial for maintaining a clean and organized project history. Merging allows you to integrate changes from different branches, while squashing lets you consolidate multiple commits into one. This not only simplifies the commit history but also makes it easier to understand the evolution of a project.
In this tutorial, we will explore the methods to merge and squash commits in Git, providing you with step-by-step instructions and practical examples. Whether you are a beginner or looking to refine your Git skills, this guide will help you navigate these essential functionalities with ease.
Understanding Merging in Git
Merging in Git is the process of combining changes from one branch into another. This is particularly useful when you’re working on a feature branch and want to incorporate the latest updates from the main branch. To perform a merge, follow these steps:
-
First, ensure you are on the branch you want to merge into. For example, if you want to merge changes into the
main
branch, switch to it using:git checkout main
-
Next, use the merge command to integrate changes from your feature branch. Replace
feature-branch
with the name of your branch:git merge feature-branch
Output:
Merge made by the 'recursive' strategy.
When you run the merge command, Git will automatically combine the changes. If there are no conflicts, you’ll see a success message. If conflicts arise, Git will notify you, and you’ll need to resolve them manually. After resolving conflicts, you can complete the merge by staging the changes and committing them.
Merging is a powerful feature, but it can lead to a cluttered commit history if not managed properly. For this reason, many developers opt to squash commits before merging.
Squashing Commits in Git
Squashing commits is a technique used to combine multiple commits into a single commit. This is particularly useful when you want to tidy up your commit history before merging into the main branch. To squash commits, you typically use an interactive rebase. Here’s how you can do it:
-
First, identify the number of commits you want to squash. You can do this by running:
git log
This command will display your commit history. Take note of the commit hashes or the number of commits you want to squash.
-
Next, initiate an interactive rebase. If you want to squash the last three commits, use:
git rebase -i HEAD~3
Output:
pick 1234567 Commit message 1
squash 2345678 Commit message 2
squash 3456789 Commit message 3
In the interactive rebase interface, you’ll see a list of commits. The first commit will be kept as is, while the subsequent commits should be marked as squash
. This tells Git to combine them into the first commit.
- After saving and closing the editor, Git will prompt you to edit the commit message for the new squashed commit. You can modify it to reflect the changes made across all the commits.
Squashing commits not only keeps your commit history clean but also makes it easier for others to understand the changes made in your project. It’s a best practice that many teams adopt to maintain a clear and concise project history.
Best Practices for Merging and Squashing
When working with Git, it’s essential to follow best practices for merging and squashing commits. Here are some tips to keep in mind:
-
Commit Frequently: Make small, frequent commits that reflect logical units of work. This practice makes it easier to squash commits later and keeps your history meaningful.
-
Use Descriptive Commit Messages: Clear and descriptive commit messages help collaborators understand the purpose of each commit. This practice is especially important when squashing commits, as the final message should summarize the combined changes.
-
Rebase Before Merging: Before merging a feature branch into the main branch, consider rebasing it first. This action helps to incorporate the latest changes from the main branch and can simplify the merge process.
-
Test After Merging: Always run tests after merging or squashing commits to ensure that the code works as expected. This step is crucial to avoid introducing bugs into the main branch.
By following these best practices, you can maintain a clean and efficient Git workflow, enabling smoother collaboration and easier project management.
Conclusion
Merging and squashing commits in Git are essential skills for any developer looking to maintain a clean and organized project history. By mastering these techniques, you can streamline your workflow, making it easier to collaborate with others and manage your codebase effectively. Remember to commit frequently, use descriptive messages, and test your code after merging. As you become more comfortable with these Git functionalities, you’ll find that your development process becomes more efficient and enjoyable.
FAQ
- What is the difference between merging and squashing in Git?
Merging combines changes from one branch into another, while squashing combines multiple commits into a single commit, simplifying the commit history.
-
Can I squash commits after pushing them to a remote repository?
Yes, but it’s not recommended to rewrite history on shared branches. If you must, communicate with your team to avoid conflicts. -
How do I resolve conflicts during a merge?
Git will mark the conflicting files. You need to manually edit them, resolve the conflicts, stage the changes, and then complete the merge. -
Is it possible to undo a merge in Git?
Yes, you can usegit merge --abort
to undo a merge if you haven’t committed it yet. If you have committed, you can usegit reset
to revert to the previous state. -
What command do I use to check my commit history?
You can usegit log
to view your commit history, which shows all commits made in the current branch.
Abdul is a software engineer with an architect background and a passion for full-stack web development with eight years of professional experience in analysis, design, development, implementation, performance tuning, and implementation of business applications.
LinkedIn