DEV Community

Cover image for Git Squash Simplified
Pawel
Pawel

Posted on

Git Squash Simplified

If you have just started as an open source contributor or planning to become one, you might came across the open source projects which makes you to squash all the commits into a single one before or after opening a pull request.

And as a beginner, It might feel challenging to you as the fear of messing up with other's commits can be there but all of this can be eliminated by using correct tools and extensions.

In this post I'll help you to get over this fear will also help you to master and get comfortable with squashing commits.

Let's start with the definition itself.

What is squashing commits in git means?

To "squash" in Git means to combine multiple commits into a single one.

When to squash the commits?

You can squash commits at any point in the time but it's highly recommended to squash the commits while there's a correction or suggestion being done and you don't want to add an extra commit into the git history stating the corrections you've made.

Which tools should I use to make it more easier and errorless?

I would recommend using VSCode as your code editor with the GitLens extension installed. In this tutorial I'll be using the same.


So Let's get started!

1. First you have to change your default code editor for Git.

Git comes with nano as its default code editor, In order to make use of the GitLens extension you've to change the Git's defaut code editor to Visual Studio Code.
To do so enter the command in the terminal



 git config --global core.editor "code --wait"


Enter fullscreen mode Exit fullscreen mode



This will change your default code editor in git from nano to vscode.

Now you're all set.

Let's say you have made a correction to your code and committed the changes. After doing so let's get the overview of the commits you want to squash.
To enlist the commits type below command in the terminal (Make sure you're in the same directory/git repository you want the squash the commits in.)



git log --oneline


Enter fullscreen mode Exit fullscreen mode

Here's my output

Git Log
Theme :- JellyFish

Now Let's squash those commits

In order to squash the commits we don't have any explicit command like git squash or anything like that in git rather we use the rebase method to achieve the squashing of commits.

So, To squash the commits you made type in the command:



git rebase -i HEAD~N


Enter fullscreen mode Exit fullscreen mode

where N is the number of commits you want to squash

I'll be squashing recent 2 commits so that's how my terminal will look like

Git Rebase
And After hitting the Enter Key

RebaseWindow
Theme :- JellyFish

Select squash from the dropdown for the commits you want to squash
SquashDropdown

and select the reword from the dropdown in order to edit the commit message while squashing.

After selecting the options your vscode window may look like this

Reword
Theme :- JellyFish

Start Rebase

Now let's start the rebasing by hitting on Start Rebase



As soon as you hit the Start Rebase button a new commit message tab will open in the editor. This is why we've set vscode as the default code editor for the Git in order to get this tab open up in the vscode itself which will further make it more easier to edit the commit messages without any hussle of remembering the shortcut keys for the nano code editor this will completely eliminate those steps making your workflow more optimized and efficient.

Since we've chose the reword option for the previous commit it will ask us to edit the commit message for that commit message.



Reword Commit Message

I will be keeping it as it is, so press Ctrl+S or Cmd+S (MacOS users)
As soon as you save and close this tab the new rebase commit tab will open which will again confirm the messages you wanted to have in the squashed commit.

Commit Message Rebase

Again I will be keeping it as it is, so press Ctrl+S or Cmd+S (MacOS users) and close the tab.



Hurray! You've squashed the commits. Congrats🎉
squased output

It can be seen by typing in the following command in the terminal again



git log --oneline


Enter fullscreen mode Exit fullscreen mode

and here's the output

Squashed

You can see the Fix Bugs commit has got squashed and as we haven't changed the previous commit it remained same. And when you type git log it will show you both the messages remembered that second commit message tab? It came from there.

Vote of Thanks

Thank you so much for reading this post and I hope this post will help you to squash your commits even more easier and faster. Feel Free to give any suggestions and if you like my work you can connect with me via Twitter or LinkedIn

Have a great day 😊

Top comments (10)

Collapse
 
abumuhab profile image
Abdulmalik

Really helpful. Thank you

Collapse
 
drumstickz64 profile image
Drumstickz64

I'm confused. Isn't there a --squash flag for git commit?

Collapse
 
pb profile image
Pawel • Edited

Hey @drumstickz64 thanks for asking this question. There's a command git merge --squash <branchname> which can be used to squash the commits of a particular branch but it won't perform an automatic commit you've to perform the commit after squashing. This method can bring you conflicts which you have to resolve manually. This method can be used when you've to squash a large amount of commits of a particular branch into a single one and then merge it into the master branch. This method will be faster than the interactive rebase method. Although interactive rebase method provide you the flexibility to fine tuned your commits and as a beginner you won't feel intimidating as you can see what you are doing as it brings the editor for you.

Collapse
 
drumstickz64 profile image
Drumstickz64 • Edited

I see. I can definitely see situations where the interactive rebase method could be useful. Thank for the explanation. 👍

Collapse
 
froxx93 profile image
Froxx93

I use squash and interactive rebase in my every day work for multiple years now, and everything got so much cleaner with it.

No more commit like:
Awesome feature
Bugfix
Real bugfix
Bugfix3
Final bugfix
Dang, help!!11

Commits like that are not helpful to anyone, and since I'm driving a "1 commit equals 1 feature"-policy, not only I but especially my coworkers understand my work much better.

Collapse
 
pb profile image
Pawel

I'm glad it was helpful and thanks for sharing your experience with interactive rebase and the one commit equals one feature policy is great.

Collapse
 
zaxwebs profile image
Zack Webster

Good to know, thanks for sharing this.

Collapse
 
pb profile image
Pawel

You're welcome Zack ☺️

Collapse
 
leandrodiascarvalho profile image
leandro dias de carvalho

Obrigado pelas dicas.

Collapse
 
pb profile image
Pawel

O prazer é meu 😊