DEV Community

Rohit Patil
Rohit Patil

Posted on

To Squash or Not to Squash, That Is the Question

Git Squash @studytonight
Git Squash @studytonight

Git squash is a controversial topic in the software development community. Some people believe that it is a valuable tool for cleaning up commit history, while others believe that it can lead to information loss and should be avoided.

In this article, we will take a closer look at the pros and cons of git squash. We will also discuss some best practices for using git squash, if you choose to do so.

What is Git squash?

First, it is important to understand what git squash does. When you squash commits, you are essentially merging multiple commits into a single commit.

Here are some situations where you might want to use Git squash:

  • When you have a series of small commits that are all related to the same change. For example, you might have a series of commits that fix a bug, or a series of commits that add a new feature.
  • When you want to clean up your commit history before merging your branch into another branch. This can make it easier for other developers to understand your changes.
  • When you want to create a more readable and concise commit history for a public repository.

How to Squash commits?

To squash commits, you use the git rebase -i command. This will open a text editor where you can edit the list of commits in your current branch. To squash two commits together, simply change the pick command to squash.

Once you have made your changes, save the file and exit the editor. Git will then replay your commits, squashing them together as you specified.

Reference: How to Squash Commits in Git

Why use Git squash commits?

  • Cleaning up your commit history: Git squash can be used to combine multiple commits into a single commit, which can make your commit history more readable and concise. This can be especially useful if you have a series of small commits that are all related to the same change.
  • Making your changes more visible: Squashing your commits can make your changes more visible to other developers, especially if you are working on a team project. This is because a squashed commit will have a single commit message that summarizes all of the changes that were made in the original commits.
  • Making it easier to revert your changes: If you need to revert your changes, squashing your commits can make it easier to do so. This is because you will only need to revert the single squashed commit, instead of reverting all of the original commits.

Why not use Git squash commits?

  • Loss of information: When you squash commits, you are essentially deleting the original commits. This can lead to a loss of information, such as the original commit messages and the commit timestamps.

  • Difficulty tracking down bugs: If you squash a commit that contains a bug, it can be difficult to track down the source of the bug. This is because the squashed commit will contain all of the changes from the original commits, making it difficult to isolate the change that introduced the bug.

  • Confusion for other developers: If you squash commits after they have been pushed to a public repository, it can make it difficult for other developers to understand your changes. This is because the other developers will only see the squashed commit, which may not be clear or concise.

  • Risk of conflicts: If you are working on a team and you squash commits before merging your branch, it can increase the risk of merge conflicts. This is because the other developers may have made changes to the same code that you have changed.

How to squash Git commits effectively?

  • Only squash commits that are related to the same change. For example, you might have a series of commits that fix a bug, or a series of commits that add a new feature.
  • Only squash commits that are still in your local repository. Squashing commits after they have been pushed to a public repository can make it difficult for other developers to understand your changes.
  • Create a meaningful commit message for the squashed commit. The commit message should summarize all of the changes that were made in the original commits.
  • Let your team members know that you are squashing commits before you do so. This is especially important if you are working on a team project.

Here are some additional tips for squashing Git commits:

  • Use a descriptive commit message for each original commit. This will make it easier to write a meaningful commit message for the squashed commit.
  • Use the interactive rebase editor to squash your commits. This will give you more control over how your commits are squashed.
  • Test your code after squashing your commits. This is to make sure that the squashed commits do not introduce any new bugs.

Conclusion

Overall, Git squash commits can be a useful tool for cleaning up your commit history, but it is important to use them carefully and to be aware of the potential drawbacks.

Here are some additional things to consider when deciding whether or not to use Git squash commits:

  • The size and complexity of your project: If you are working on a large and complex project, it may be more difficult to track down bugs and merge conflicts if you squash your commits.
  • The team culture: If you are working on a team where there is no consensus on whether or not to use Git squash, it is best to avoid using them.
  • The purpose of your repository: If you are working on a public repository that is used by other developers, it is best to avoid squashing your commits. If you are unsure whether or not to use Git squash commits, it is always best to err on the side of caution and avoid using them.

Top comments (2)

Collapse
 
cappe987 profile image
Casper

This is good advice for squashing. Clean up the git history after developing a feature. I hate seeing a bunch of commits like "Fixed thing" that only makes sense in the context of the whole feature development. But squashing a huge feature into one commit loses a lot of information. Best case is if you can clean it up into several logical commits.

Squashing/rebasing is fine even after pushing, but that is assuming you are working on a branch by yourself. If someone else is using that branch it is not a good idea to force push.

A clean git history is essential when debugging or trying to understand why a decision was made.

Collapse
 
nsemikin profile image
n-semikin

That's the point. Squashing commits you may loose history why some decision was made. For example if a developer tried some approach1 and after its implementation found that there is more effective way and implemented approach2, then squashing commits will remove this history. And some guy in the future may think "why the hell that dude implemented like this - there is much more simple approach1".

Another drawback is a Mega-commit that is very hard to review hundreds of changed files. If the feature history is structured by steps, like 1,2,3 then it's easier to check them one by one.

So I think squashing is not the "always do" option. You should check if it rely makes the history cleaner before doing it.