Skip to content
Related Articles

Related Articles

Git – Squash

Improve Article
Save Article
  • Last Updated : 02 Mar, 2022
Improve Article
Save Article

Assume you are building a project using Git as your version control. You released Version 1 after that you wanted to add new features for the Version 2 release and you even fixed a few bugs found in Version 1. You had plenty of commits piled up after your 1st release. Is there a way to merge all the commits after 1st release into a single commit? YES, it is possible using squash.

Squash is one of the useful and powerful features available in the git rebase command’s interactive mode.

Git Squash

Now we have a repository called GFG_VIDEO, which is an open-source video conferencing tool. GFG_VIDEO has released the 1st version of their tool with basic features such as peer-to-peer video calling and messages with tag R_V1 (Green colored). After the R_V1 release team GFG_VIDEO started to add new features such as creating groups, group video calls, and fixing minor bugs from R_V1 such as call drops, etc. Now GFG_VIDEO is ready for their new release R_V2. If you observe, we have 3 commits from our initial commit to R_V1 (1st Release). After our R_V1 we have 3 commits for our R_V2 (2nd Release), It kind of looks untidy and difficult to follow. Here we can use the Squash concept and merge all the commits after R_V1 till R_V2 to a single commit which makes our repository log more tidy and easy to follow.

Git Squash - Intial Commit

Implementation: Focusing on squash command.

GFG_VIDEO commit log After Release R_V1

The above image shows we have 3 commits: Initial commit, Commit 2, Version 1 Release. We’ve successfully released the 1st version (R_V1) of the GFG_VIDEO tool. After R_V1 new features are added and minor bugs are fixed from the previous release and the tool is ready for its 2nd release R_V2.

GFG_VIDEO commit log After Release R_V2

The above image of the GFG_VIDEO log is after the 2nd version release. It can be observed after Version 1 Release (tag: R_V1) there are 3 commits for Version 2 Release. This kind of looks untidy, to make it simpler to read we can do a squash operation.

Let’s perform squash now

git rebase -i HEAD~3 

rebase is an action to rewrite commits and it’s history -i is to enter into interactive mode of rebase HEAD~n states to perform our operation on n commits from HEAD

Upon entering the above command we’ll get an interactive editor with all our selected commits which is where we’ll be performing squash.

Interactive Editor Upon Entering Rebase Command 

We can see we have selected 3 commits are at the beginning of the interactive editor, below that we can see the commands list such as pick, reword, edit, squash, etc. 

Task: To squash  2nd and 3rd commits with 1st commit, so we’ll change the first word from pick to squash.

whichever commits we want to squash we have to change it to squash from pick

Changed from pick to squash

After changing commits from pick to squash save the file and close it, Immediately another editor will be opened where we have to enter the latest commit message. 

Interactive Editor For Entering New Commit Message

Enter the latest commit message and comment on remaining old messages.

Latest Commit Message Added

After adding the latest commit message save the file and exit the file. Now it shows rebasing is successful.

Successful Message

Now if we see our GFG_VIDEO log we can observe our 3 commits after version 1 release are squashed into 1 commit.

Squash Operation Completed

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!