Open In App

Git Squash

Last Updated : 06 Oct, 2025
Comments
Improve
Suggest changes
7 Likes
Like
Report

Git squash takes several commits, especially on feature branches with many small incremental commits, and combines them into one. This prevents clutter and presents a polished, easy-to-follow history to your team.

squashing_commit
Git Squash

Git Squash-Merge Command Line

You can combine commits from a feature branch into a single commit and merge it into your current branch using:

git merge --squash feature-branch

This command condenses all commits from the feature branch into one that is applied onto your current branch. After squash merging, the repository history becomes cleaner and more organized.

How to Squash Commits

There are mainly two way:

1. Interactive Rebase Method

  • Run:
git rebase -i HEAD~3
  • An interactive editor opens listing the last 3 commits.
  • Change pick to squash on commits you want to combine into the first one.
  • Save and close the editor.
  • Another editor opens where you can write a combined commit message.
  • Save and exit to complete rebasing.

After this, your multiple commits will be combined into one.

2. Squashing with the --squash Option

  • Create and switch to your target branch:
git checkout main
  • Merge with squash option:
git merge --squash feature-branch
  • Resolve any merge conflicts manually.
  • Commit combined changes with a message:
git commit -m "Add group video calls and bug fixes"
  • Push changes to the remote repository.

Squashing Last Two Commits

If you want to squash only the last two commits:

  • Switch to your branch.
  • Run:
git rebase -i HEAD~2
  • Change pick to squash for the last commit.
  • Save, update commit message as needed, and close the editor.

Git Squash vs Rebase

Git SquashGit Rebase
You can combine multiple commits into a single commit.Applies commits on top of the different base commits.
After using git squash your commit history will be more clean and organized.You can form a new branch by using previous commits. 
Must be done on private branches. Rebasing can be done on a feature branch or shared branch.
By using squishing you can maintain clean and logical commit history.By using rebase command you can keep your branch up to date.


Suggested Quiz
5 Questions

What is the purpose of Git Squash?

  • A

    To delete commits permanently

  • B

    To combine multiple commits into one

  • C

    To merge branches without conflicts

  • D

    To clone repositories faster

Explanation:

Git squash compresses multiple commits into a single meaningful commit.

Which command performs squash merging?

  • A

    git merge feature-branch

  • B

    git merge --squash feature-branch

  • C

    git rebase feature-branch

  • D

    git push --squash

Explanation:

--squash option merges all commits as one combined commit.

Which command is used to squash commits using interactive rebase?

  • A

    git rebase -i HEAD~3

  • B

    git squash HEAD~3

  • C

    git merge HEAD~3

  • D

    git reset -i main

Explanation:

Rebase interactive allows editing commit list where pick → squash.

After squashing with merge, what must be done next to finalize changes?

  • A

    Delete the branch

  • B

    Force push the commits

  • C

    Run git commit manually

  • D

    Run git checkout main

Explanation:

git merge --squash only stages combined changes — you must commit them.

Squash should ideally be used on which type of branches?

  • A

    Public shared branches

  • B

    Private feature branches

  • C

    Remote-only branches

  • D

    Production branches only

Explanation:

Squashing rewrites commit history → safe only on private branches.

Quiz Completed Successfully
Your Score :   2/5
Accuracy :  0%
Login to View Explanation
1/5 1/5 < Previous Next >

Article Tags :

Explore