Git Squash: Keep Clean of Your Git Commit History

 Imagine, you are assigned to write a new feature of the main product. How do you start? You copy the source code, start coding for feature, write, test, write, fix bugs selfly explored, optimize code, fix typos or such minor task,s and time to time you have to commit your changes so that you don’t lose the valuable work you have done for the feature.


So, we can guess, you may have commits of test code, some type fixing commit, commit of missing comments etc. At last, you have completed your feature!


Wow! Great work! But wait! You want to have a look at your commit history and applied:

git log - -oneline


Facepalm situation! You got all your commits including minor changes, typos, bug fixing, code-comment all types of commits in separate commits! How the hell shall I clean my messy commit history?

Feels awkward, right?



Noworries buddy! Git has given us the power to present them nicely, combined them into a single commit. This way, you can group your commits so that you don’t have to find a needle(important commit/change) in a haystack(irrelevant commits). Let’s learn this in a simple way- git interactive way.

Let’s say we have a repo. Our master branch has these commits:


git checkout -b feature


Now, commit several commits one after another:

feature created //first commit

feature just modified //second one

feature modification 2 //..

feature final-done //final commit

log the commit history:

You can see that we have some minor commits of the feature branch. Now, our goal is to make all the four feature commits to one commit.

We will use git-interactive way which is simple yet powerful.

Command format:

git rebase -i HEAD~N
i-> interactive
N-> number of commits starting from the current head

In your case, we got 4 commits to be merged. So apply this:

git rebase -i HEAD-4

you will see like this editor:


Notice that, the oldest commit is in the first position and then the newer ones.

So, when you are ready to squash commits, you have to replace the pick with s or squash except the oldest one from which you want to squash your commits. In this case, “feature created” is the oldest commit and we want to squash from this commit[including] to “feature final-done” commit, so our editor will be like this:

To edit: press “i” //vim instruction to insert





Now, save and exit the editor. In my case it’s vim editor, so press “ESC” and then :x and press enter.

After that, you will be moved to another popup editor to set a new commit message for your squash commit. You can set new message or quit to set the default one. I set new message as

“squashed 4 feature commits by this”

Now, check the commit log and this is what you will get:

You see, all the 4 feature commits are gone, instead, we got our squashed commit on top of that!

Now, checkout to master and merge

git checkout master

git merge feature

git log — oneline

You will see this:


So, this is pretty simple, isn’t it?

Keep calm, do coding, clean your commit history!

                                 Happy Coding!

Comments

Popular posts from this blog

Java with MINIO file operations: upload, download, delete

Spring Boot Scheduler for Distributed System: Using shedlock

Kafka Stream API: MySQL CDC to apache Kafka with debezium