Git Squash: Squishy Commits for a Cleaner Code Pasture!

Git Squash: Squishy Commits for a Cleaner Code Pasture!

Many commits can be made in a single branch. Git Squash is the technique we can use to combine all these commits as one which will lead to maintaining a clear commit history.

Follow these simple steps:

Let's create a repo on GitHub


Clone the repo and add a change


Make the first commit

Type these commands to make the first commit.

git add .
git commit -m 'first commit'

Type this command to see your git history.

git log

Commit history should look like this. There you can see the latest commit that we made.


Make another commit

Changes.

Commit history.


Repeat again

Changes.

Commit history.


Now let's try to combine the first, second and third commits

Run this command to start the squshing process.

git rebase -i HEAD~3

We are considering the last three commits. Therefore, we use HEAD~3.

After running above, you should see this.

Pick the commits you are going to squash. In our case, it is the second and the third. Mark them like the below. Remove the pick and add s in front of each commit you need to squash.

Now save it and exit. (On Ubuntu terminal it is ctrl+x and type y).

Now you will see a screen like this. Change the commit message here.

Let's only keep the message for the first commit and save. If it is successful you will see a message like below.


Verify the commit history and changes

You should only see the first commit with all the changes you made in different commits.