Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags


How to squash commits in Git

Educative Answers Team

git squash combines several commits into one. The primary reason for doing this is that a lot of commit history is only relevant for the developer who generated it; so, it must be simplified before it is pushed to a shared repository.

Consider this ​example in which three commits, made after the commit labeled “START”, need to be squashed into one:

svg viewer

​Let’s consider the above example in a more practical context by looking at the following git log:

* df71a27 - (HEAD feature_x) Fixed minor bugs (3 minutes ago)
* ba9dd9a - Changed CSS (25 minutes ago)
* f392171 - Added new feature X (1 day ago)
* d7322aa - (origin/feature_x) Pre-reqs for feature X (4 days ago)

You have added a feature X and followed it with two more commits to fix the CSS and some bugs. Now, in order to make this commit history concise, the last three commits need to be squashed into one.

git rebase -i HEAD~3

This will open up an editor with the following text:

pick f392171 Added new feature X
pick ba9dd9a Changed CSS
pick df71a27 Fixed minor bugs

Here, you can decide what to do with each commit. Let’s keep the commit f392171 and squash the other two into one.

This is done by changing the file as follows:

pick f392171 Added new feature X
squash ba9dd9a Changed CSS
squash df71a27 Fixed minor bugs

Saving and closing the editor after making the above changes will successfully squash the commits into one.


Copyright ©2022 Educative, Inc. All rights reserved

View all Courses

Keep Exploring