Git Squash

Git Squash

How to Squash?

The squash command in Git is used to suppress or subdue the previous commits into one unit. It is quite interesting to note that squash is not a command in Git, instead, it is a keyword. Squash keyword is generally used to implement group-specific changes before moving them to others. The power of squash is to merge several commits into a single commit by interactive rebase feature.

Git Squash

If you are a Git user, then you must have realized the importance of squashing a commit. Especially if you are an open-source contributor, then many times, you have to create a PR (pull request) with squashed commit. You can also squash commits if you have already created a PR.

A Git user must have known the importance of squashing a commit. Those who contribute to the open-source projects have a clear idea that they need to create a pull request with the squashed commit. Squashing can be done at any point in time if you already have created a pull request.

Note: There exists no stand-alone squash command in Git provided it can only be apt to use while merging or rebasing.

To squash, we first check the commit history using the following command:

$ git log --oneline  

This command gives out the log of all the commits we want to squash.

Suppose we want to squash the last commit, we may type the command:

$ git rebase -i HEAD ~ 4

This command opens up your default editor and would squash the last four commands.

If you want to merge all four commits into one, you need to replace the pick command with squash and then exist the editor. The outputs on your console would appear something like this:

 $ git rebase-i Head ~4
 4 files changed
 Successfully rebased and updated  refs/head/master. 

When to Squash?

For instance, you may have worked on a project and might have generated plenty of commits for your reference. At some point in time, you may surely want to commit and consequently merge your work to the master branch. This is one situation where you need to squash all the commits into one so that all the previous as well the current commits can be merged into one. Therefore, you decide to squash when:

If you want to merge individual commits from the branch and integrate them into one single commit. You would end up having one single commit for all other commits.

If you want to go against squashing and all your commits remain intact as such. If some teams think squashing might be an advantage, they may use it while the other would opt to go against it. Squashing is a great feature that enables teams to collaborate if working in a single workspace without bothering about overwhelming and unnecessary multiple commits.

However, the above points don't need to favor all the situations. It is dependent on the type and the nature of the project you might be working or simply the team may or may not use a single branch for their project.  

Squash Merge and PR

Merging is a technique similar to squash which can be contrarily used with squash. It also has similar features and commands like squash. Therefore, squash merge will combine all the branches of individual commits into one. It can be done by the following command:

 $ git merge --squash feature/login
 Auto-merging imprint.html
 Removing img/iconBlog.png
 Auto-merging about.html
 Squash commit -- not updating HEAD
 Automatic merge went well; stopped before committing as requested 

You are now left with your local changes in the working copy which you can now commit yourself.

Now, you can easily push our single commit to the master branch with the help of the following command:

$ git push -origin master                      // general push

Or

$ git push -f origin master                  // force push

Creating PR

Now that you have merged and been confronted with the master branch, you can create a pull request. This can be done from third-party version control systems like GitHub, BitBucket, or GitLab.

One important fact to remember is when you have merged using squash, the pull request is closed. Therefore, you need to manually choose an option present in GitHub(recommended) and merge and squash all the individual branches into one. You would see something like this:

Git Squash