JIYIK CN >

Current Location:Home > Learning > OPERATING SYSTEM > Git >

Git squash all commits

Author:JIYIK Last Updated:2025/03/31 Views:

In every developer’s life, the word squash is often used while working with the Git distributed control system . This feature in Git is a handy option that developers often use to achieve a neat workflow in their development team.

In this section, we will discuss in detail the main feature of Git i.e. Squashing. Also, the process of squashing and why we need it when working with a development team.


Git compression

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

In Git, the term squash is used to combine various commits into a single commit using the command line. This feature keeps things in the correct order of first-in, first-out.

Let's use the following example to explain squashing and sorting order in commits.

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


 After Squashing commits B, C, and D:

     A ◄───── E


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

Suppose we merge all commits into a new commit E. Now commit E contains the changes made in commits B, C, and D.

Compaction is done specifically to keep the branch graph clean over a longer life cycle.

When working on a new feature in an application, it is obvious that we will make several commits before we get the desired result. This could be some fixes to a bug reported by the QA team or some tests.

After applying these features, we have collected some insignificant commits that make our branch look messy. For this case, we will do a squash in this repository branch.

It will help us merge these redundant commits into one.

The important thing to remember here is that squash is not a Git command. However, it is an essential Git operation.

If we execute it git squash, it will give an error because it is just an operation that can be performed through the Git interactive rebase command.


Squash all commits with Git Interactive Rebase

With Git's interactive rebase feature, we can manually squash our commits at any point in the branch lifecycle. Let's get started by executing the following command using the alias slog, which will help us view a 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 the related commits in the default editor along with the sorted order. Here, we want to squash these commits, control them and save them in the editor using Git commands.

Here is the command to squash the last X commits:

$ git rebase -i HEAD~[X]Copy

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

$ git rebase -i HEAD~4Copy

When we say last X commits, it means last x commits from beginning to end.

As a result of the interactive rebase, Git's default editor starts and begins squashing the commits we want in one commit. The commits listed using the command pick are the ones we want to squash.

Now, we will change the command for the commits, pick, to s or squash, so that these commits will be squashed.

After that, we will save the changes and close the editor. git rebaseThe operation will be performed as we have indicated.

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

The Rebase was successfully executed. To satisfy ourselves, we can git slogview our commit log by executing the command again, as shown below:

$ 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 output window above, the last 4 commits were squashed into one, and it was easy for the developer to merge all four different commits into one feature commit.

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Close the Git commit editor on Windows

Publish Date:2025/03/31 Views:62 Category:Git

In this article, we will discuss how to exit the Git commit editor. This can be a little tricky, especially if you are new to Git bash . Let's see how to exit the editor on Windows. Close the Git commit editor on Windows We will look at a t

Understand the Git commit sign-off function

Publish Date:2025/03/31 Views:160 Category:Git

In this article, we will explore the Git commit sign-off feature. This feature allows us to add a line containing our full name and email address to our commits. Signature confirming ownership and permission to submit submission. Understand

Add the file to the last commit in Git

Publish Date:2025/03/31 Views:176 Category:Git

This article outlines the process of adding a file to the last commit in Git. This comes in handy when you forgot to include a file in the last commit and don't want to create a new file. Let’s get straight to the point. Add the file to t

Git search commit messages using the command line

Publish Date:2025/03/31 Views:157 Category:Git

We can format git log the command to show commits with commit messages that match a specified pattern. This makes it easier when you want to find a commit but your repository has hundreds of commits. This article will discuss the process of

Modifying a specific commit in Git

Publish Date:2025/03/31 Views:100 Category:Git

This article explains how we can modify a specific commit in Git. We may need to rename, compress, edit, or add files to a commit. The best way is to use the git rebase command in interactive mode . What do you think of this? Modifying a sp

Git overwrites Master with branch

Publish Date:2025/03/31 Views:79 Category:Git

Git is used to keep track of the source code we are working with; it also facilitates collaboration and helps us keep our projects in their current state. As we develop new features, their history should be at our fingertips as it is very h

Git Squash Commits

Publish Date:2025/03/31 Views:162 Category:Git

We will learn about Git squashing in this tutorial. The basic idea is to merge multiple consecutive commits into one. The main purpose is to compress many commits into a few related commits. Thus, doing so will make the git history look con

Merge remote branches into local branches in Git

Publish Date:2025/03/31 Views:134 Category:Git

This tutorial will merge a remote git branch into a local branch by cloning the remote repository and updating the changes locally. Merge remote branches into local branches in Git by cloning the remote repository and updating the changes l

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial