How to Squash All Commits in Git

Abdul Jabbar Feb 02, 2024
  1. Git Squashing
  2. Use Git Interactive Rebase to Squash All Commits
How to Squash All Commits in Git

In every developer’s life, the word squash is frequently used while working with Git distributed control system. This function in Git is a convenient option and is often used by developers for a neat workflow in a team of developers.

In this block, we will talk in detail about the main feature of Git, i.e., Squashing. Furthermore, the process of squashing and why we need it while working with the team of developers.

Git Squashing

First, we need to know what squashing is. Generally, squashing is mixing something with all the available things.

In Git, the term squash is used for combining various commits into one commit using the command line. And this feature keeps the thing in the correct order as first in, first out.

Let’s explain the squashing and sequencing order in commits with an example below.

     A ◄───── B ◄──── C ◄───── D


 After Squashing commits B, C, and D:

     A ◄───── E


  Commit E includes the commits B, C, and D changes.

Let’s say we will merge all the commits into a new commit E. Now commit E comprises changes made in commits B, C, and D.

Squashing is specially done to keep the branch graph tidy in a longer life cycle.

When working with a new feature in an application, it is obvious that we make a couple of commits before getting the desired result. That may be some fixes of the bugs reported by the quality assurance team or some tests.

After applying these features, we have gathered some inessential commits that are making our branch look messy with commits. For this scenario, we will go with squashing in that repository branch.

It will help us combine these redundant commits into one.

The main point to be remembered here is that squash is not a Git command. However, it is an essential Git operation.

If we execute git squash, it will give us an error, as it is only an operation and can be executed through the Git interactive rebase command.

Use Git Interactive Rebase to Squash All Commits

With the interactive rebase feature of Git, we can manually squash our commits at any time we want at any point of the branch life cycle. Let’s get started by executing the below command with the alias slog, which will help us view the compact commit log.

git config --global alias.slog = log --graph --all --topo-order --pretty='format:%h %ai %s%d (%an)'

Output:

$ git slog
* ac1sd5f 2022-02-11 11:09:15 +0600 Commit D (HEAD -> master) (test)
* 5dasq6f 2022-02-11 11:09:02 +0600 Commit C (test)
* 5asa04d 2022-02-11 11:09:02 +0600 Commit B (test)
* c40as62 2022-02-11 11:10:56 +0600 Commit A (test)
* 29awqc5 2022-02-11 11:10:33 +0600 BugFix #1 (test)
* 3asafeb 2022-02-11 11:10:19 +v Feature1 implemented (test)
* cbas10d 2022-02-11 11:26:19 +0600 Init commit (test)

Here, the Git command Interactive Rebase will also show all relevant commits in the default editor with the sequencing order. Here, we want to squash these commits, control them and save them in the editor using the Git command.

Following is the command used to squash the last X commits:

git rebase -i HEAD~[X]Copy

As we want to squash the last 4 commits, we will mention 4 instead of X, as of the above command.

git rebase -i HEAD~4Copy

When we say last X commits, it means the last x commits from the head to the bottom.

As a result of the interactive rebase, Git’s default editor is started and starts squashing the commits that we wanted to be in one commit. The commit listed with the command pick is those that we want to squash.

Now, we will change the command pick of commits into s or squash, so these commits get squash.

After that, we will save our changes and close the editor. The git rebase operation will be performed as per our instructions.

$ git rebase -i HEAD~4
[detached HEAD fa29cd5] Commit A
 Date: Tue Sep 04 11:10:11 2022 +0600
 1 file changed, 1 insertion(+), 1 deletion(-)
Successfully rebased and updated refs/heads/master.

Rebase is performed successfully. For our satisfaction, we can have a look at our commit log by executing once again the git slog command as follows:

$ git slog
* f9SAEd5 2022-05-22 4:09:02  +0600 Commit A (HEAD -> master) (test)
* 29aS6c5 2022-05-22 4:09:02  +0600 BugFix #1 (test)
* 34faseb 2022-05-22 4:09:02  +0600 Feature1 implemented (test)
* cbadw0d 2022-05-22 4:09:02  +0600 Init commit (test)

As we can see in the above output window, finally, the last 4 commits have been squashed into one, and it has been easy for developers to get all four different commits to work into a single feature commit.

Author: Abdul Jabbar
Abdul Jabbar avatar Abdul Jabbar avatar

Abdul is a software engineer with an architect background and a passion for full-stack web development with eight years of professional experience in analysis, design, development, implementation, performance tuning, and implementation of business applications.

LinkedIn

Related Article - Git Squash