Git: How to squash commits

Why should you squash commits?

At workplace, developers usually have to deal with busy repositories with a lot of branches and commits. If you work on a big feature then it is easy to commit even a small change to your current branch just for the sake of saving the code. While most commits are important, some of them may irrelevant when you are ready to push your code to remote. So to mitigate those irrelevant commits and provide a clear and concise message for team members to provide readability, you should squash commits.

9525f31 Update index.html 417c8b8 update color of title according to designer dd9f205 updated color of title 8ef9271 fixed slideshow display issue 9b1ee31 Work in progress - footer work e8b2a1e hotfix for #233 225c383 implemented feature #12 823efac minor translation typo fix

Now above commit while developing a feature, it is fine, but some of them are not necessary to keep in the remote work history because they can be incorporated in one commit. In order to do that, you should use squashing commits.

Important: You should only squash commits with your local branch with linear history of commits. In another words, you must not squash commits on public branch. Your private feature branch with only your commits in a linear history should be squashed.


How to squash commits?

It is also known as interactive rebase. You need the following command to squash commits...

git rebase -i HEAD~<n>

or
git rebase -i <after-this-commit-sha1-hash-value>
The ‘ -i ’ flag in the above example indicates that this will be an interactive rebase with the ‘<n>’ designating the number of commits that you wish to rebase.

So for example, let's see this is the history of commits look like at first ...



So in case we want to merge all 4 commits shown in the picture, you can run command like this 

git rebase -i HEAD~4

Which means that from HEAD ( head means the first commit on a branch, in another words, the most recent one) to 6 commits down (head inclusive).

Or the alternative and equivalent of the above command would be

git rebease -i 011917d

which will pick up commits from SH1 hash to above till HEAD. 
One you run that command, you will be presented with a screen like this...

Git clearly indicates what word you should write in order to squash them and/or reword or pick them. If you want to. In order to edit this, you can press "i"  to edit this file, so you will see "--INSERT--"  in the bottom of the editor just like the above screenshot.

After this, you can choose do the following...


So as you can see, here I chose to reword my first commit and then squash all others. You must select one commit with PICK or REWORD or git wouldn't allow you to rebase because git needs atleast one commit to squash all commits. 

After you have made necessary edits, you can press Esc, and then :x  to exit and save the necessary edition. 

Since I have chosen REWORD git will present me with a chance to reword my commit. 

This is the message that will appear after we squash all commits together indicated in yellow color at the top of the screenshot.
Once you exit and save those changes, you will be presented with a message like this..

Congratulations, you have successfully squashed all commits into one, now your commit history would look like this.


You can see that all 4 commits are squashed into one in above screenshot with reworded message that we provided.

Hope this tutorial has helped you to understand how to squash commits. 


Post a Comment

0 Comments