JIYIK CN >

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

Git Squash Commits

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

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 concise and clear.

Another way to look at it is that we make multiple commits for a certain task. After a while, when we reach a satisfactory state, many commit messages clutter the git history.

At this point, we may want to merge the different commits into one so that the git history looks clean and best reflects what has been accomplished.

Another use case is squashing when merging branches. Usually, we create a feature branch from the main branch for some feature development.

Once the feature is completed, we merge the feature branch into the master branch. Here, we may also want to squash the various commit messages done in the feature branch into one when merging into the master branch.

Note that there is no git squashcommand.

There are two ways to implement Git compression:

  • git rebase -iAs an interactive tool for squashing commits
  • git merge -squash-squashUse the option when merging

git rebaseSquash Git commits using the interactive tool

Consider the following git log excerpt, which shows HEADthe last four commits that we are interested in squashing.

25c38c4 remove .class files
da66e6a Delete version.ini
f4e3f09 Delete .log
b0e6655 Delete .lock
da66e6a github git notes

We can see the first four commit messages in the log, representing the actions of deleting different unrelated files. Now, we will merge these four commits into one.

Following is the syntax of the command to squash the last X commits using the interactive rebase tool.

git rebase -i HEAD~[X]

So, to squash the four commits, we would do the following.

$ git rebase -i HEAD~4

After issuing this command, Git will invoke the default editor with the details of the commit to squash as shown below.

pick b0e6655 Delete .lock
pick f4e3f09 Delete .log 
pick da66e6a Delete version.ini
pick 25c38c4 remove .class files

# Rebase 652d2fe..25c38c4 onto 652d2fe (4 command(s))
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
# d, drop = remove commit
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out

The editor pickdisplays the various commits using the command. It also displays information about the available commands. We will use the squash(or s) command.

As shown below, we will pickkeep the first commit using the -p (for squash) command and change the remaining three commits from pick-p s(for squash) command.

pick b0e6655 Delete .lock
s f4e3f09 Delete .log 
s da66e6a Delete version.ini
s 25c38c4 remove .class files

# Rebase 652d2fe..25c38c4 onto 652d2fe (4 command(s))
#
...

Commits marked with squash( or s) will be merged into the master commit i.e. pickthe one marked with .

Now, we will save the changes in the editor and exit. After this, rebase -ithe tool will open another editor to enter the commit message, as shown below:

# This is a combination of 4 commits. The first commit's message is:

Delete .lock

# This is the 2nd commit message:

Delete .log 

# This is the 3rd commit message:

Delete version.ini

# This is the 4th commit message:

remove .class files

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date:      Sun Jan 3 16:39:23 2021 +0530
#
# interactive rebase in progress; onto 652d2fe
# Last commands done (4 commands done):
#    pick b0e6655 Delete .lock
#    s f4e3f09 Delete .log 
#    s da66e6a Delete version.ini
#    s 25c38c4 remove .class files
# No commands remaining.
# You are currently editing a commit while rebasing branch 'master' on '652d2fe'.
#
# Changes to be committed:
#       new file:   github-git-notes.txt
#

Now we will add our new commit message on top of the first commit message.

Deleted irrelevant files

# This is a combination of 4 commits. The first commit's message is:

Delete .lock

# This is the 2nd commit message:

Delete .log
...

After saving and exiting the editor, rebase -ithe tool will print the following message.

HEAD~2
Rebasing (2/2)


[detached HEAD caab6e8] Deleted irrelevant files
 Date: Sun Jan 3 16:39:23 2021 +0530
 1 file changed, 54 insertions(+)
 create mode 100644 github-git-notes.txt
Successfully rebased and updated refs/heads/master.

Now, we will check out git logand see squashedthe commit (that is) a single commit message instead of four commit messages.

$ git log --oneline
25c38c4 Deleted irrelevant files
da66e6a github git notes
...

Using git merge -squashSquashed Git Commits

Following is the command syntax to merge a branch with the current branch (usually main) and squash the source branch's commits.

git merge --squash <source_branch_name_to_squash>

We will now merge the feature branch i.e. squash it feature1with mainthe branch.

First, we'll switch to masterthe branch.

$ git checkout main
Switched to branch 'main'

We will then squashexecute using the option git mergeas shown below.

$ git merge --squash feature1
Squash commit -- not updating HEAD
Automatic merge went well; stopped before committing as requested

When we --squashexecute merge with the merge option merge, Git does not create a merge commit in the target branch as it does in a normal merge. Instead, Git takes all the changes in the source branch and feature1puts them as local changes masterin the target branch i.e. the working copy of merge.

See below.

$ git status
On branch main
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
	modified:   config.ini

Here, the files are changed in the branch config.ini.feature1

Now, all that remains is to commit the changes to mainthe branch as shown below.

$ git commit -am 'Merged and squashed the feature1 branch changes'
[main 573b923] Squashed and merged the feature1 branch
 1 file changed, 4 insertions(+)

So, we have now feature1merged the changes from the branch into mainthe branch, while squashing feature1the commit messages from the branch. We now mainhave only one commit message in the branch.

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