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

Git installation and establishment of local warehouse service

Publish Date:2025/04/05 Views:89 Category:Git

Git is a distributed version control system: the client does not only extract the latest version of the file snapshot, but also completely mirrors the original code repository. It has the following advantages: a. Since every extraction oper

git remote operation——multiple remote repositories for one project

Publish Date:2025/04/05 Views:131 Category:Git

Multiple remote repositories for a git project In our git project, the command to operate the remote repository information is $ git remote # 查看当前所有的远程仓库的名称 $ git remote -v # 查看远程仓库的名称和远程仓

Git cherry pick command usage

Publish Date:2025/04/05 Views:190 Category:Git

git cherry-pick is a powerful command that allows us to select an arbitrary Git commit by reference and attach it to the HEAD of the current working branch. Cherry picking is the act of picking a commit from one branch and applying it to an

Comparison between Git merge and Git rebase

Publish Date:2025/04/05 Views:171 Category:Git

The git rebase command may seem like Git wizardry to beginners, but if used carefully, it can actually make life easier for your development team. In this article, we compare git rebase with the related git merge command and identify all th

How to fix Git error Error: src refspec master does not match any

Publish Date:2025/04/05 Views:124 Category:Git

When using Git, we may encounter the error "src refspace master does not match any". Here's what the error means and how to fix it. What does src refspec master does not match any Mean in Git mean? We may encounter this error when we try to

Undo Git Stash

Publish Date:2025/04/04 Views:187 Category:Git

This article explains how to make and save changes to a repository. Git allows you to save changes locally and push them to a server when needed. In Git, we don't use the term save , but commit . We use git add , git commit , and git stash

View a list of cache entries in Git

Publish Date:2025/04/04 Views:59 Category:Git

We often need to pause our work and focus on something else in our development environment. Therefore, we may need to temporarily save our current work and focus on a different one. We may want to resume our original work later. git stash T

Git stores specific files

Publish Date:2025/04/04 Views:115 Category:Git

This article will cover storing changes to only specific files in Git. In Git, when we make some changes in our working tree, we may have some changes which may or may not be staged in our local repo. We may now wish to save these changes f

Scan to Read All Tech Tutorials

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

Recommended

Tags

Scan the Code
Easier Access Tutorial