Git Squash Last 2 Commits

By using the interactive rebase feature you can squash the last two commits.

For that follow the below steps: 

Step 1: Switch to the branch in which you want to squash the last two commits. Run the following command which if the interactive rebase command passes the argument Head~2 specifies you want two squash the last two commits.

git rebase -i HEAD~2

Step 2: After running the above command an interactive rebase file will open which contains the commits you are about to modify. “Pick” is used at starting of each line to squash the last commit into the second-to-last commit and changes the word from “pick” to “squash” or “s” for the last commit. 

pick <commit_hash_1> <commit_message_1>
squash <commit_hash_2> <commit_message_2>

Save and close the rebase operation will start.

Step 3: New text editor will open where you can modify the commit messages for the new squashed commit. After changing the commit message save and closing the file the rebase operation will be completed.

Git – Squash

Pre-requisite: Git

Squashing combines multiple commits into a single commit based on your commit history. With the help of squashing you can clean your branch history and can maintain an organized commit timeline. It is used before pulling requests or merging feature branches.

Similar Reads

What is Git Squashing?

Assume you are building a project using Git as your version control. You released Version One after that you wanted to add new features for the Version Two release and you even fixed a few bugs found in Version One. 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-Merge Command Line

By using the git squash-merge command you can combine multiple commits into a single commit. After using the git squash command your repository will be clean and organized manner....

When To Squash Commits?

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 the 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....

How To Squash Commits?

The below image shows we have 3 commits: Initial commit, Commit 2, and 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....

Squashing By Interactive Mode

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

Squashing By Merging With The –Squash Option

Git can combine the different changes into a single commit. The “–squash” option helps you to can merge multiple commits into a single commit....

Git Squash Last 2 Commits

By using the interactive rebase feature you can squash the last two commits....

Git Squash vs Rebase

Git Squash Git Rebase You can combine multiple commits into a single commit. Applies commits on top of the different base commits. After using git squash your commit history will be more clean and organized. You can form a new branch by using previous commits.  Must be done on private branches.  Rebasing can be done on a feature branch or shared branch. By using squishing you can maintain clean and logical commit history. By using rebase command you can keep your branch up to date....

Frequently Asked Questions

1.  Is squashing commits a good idea?...

Contact Us