Open In App

Git – Squash Commits

Improve
Improve
Like Article
Like
Save
Share
Report

In Git, squashing refers to combining two or more commits into a single commit. This is typically done to improve the organization and clarity of the commit history, especially when working on a collaborative project with other developers. In this article, we will review the feature git squashing, which is a great feature for arranging multiple commits into a single one. Git squashing is done by merging the previous **N** number of commits into a single commit. Use the git rebase -i HEAD~<no of commits to consider>. For example:

Command: git rebase -i HEAD~3

Command: git rebase -i HEAD~3

This command will open the git-rebase-todo file in your configured text editor.

Note: Please note that you cannot include the first commit with this command.

The next step is to substitute all “pick” occurrences with “squash“, except for the initial one. You will merge your changes into the first commit without losing any data.

Replace pick with squash

Replace pick with squash

Upon completion, save and close the file. Git will subsequently launch a new editor, which displays the newly generated commit message.

Newly generated commit message

Newly generated commit message

You have the option to remove all of them and insert your own personalized message.

New commit message

New commit message

Upon completion of the process, a message indicating success should be displayed in the terminal.

Successfully rebased and updated

Successfully rebased and updated

You can use the git log command to check whether your messages are successfully squashed or not. 

To undo squashing use this command:

git reset --hard HEAD@<no of commits>

Last Updated : 15 May, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads