• Skip to primary navigation
  • Skip to content

GitopsCentral

  • Home
  • Courses
  • Roadmap
  • About
  • Log In
  • Sign Up

How to squash git commits

March 7, 2019 by shaik zillani

In this blog post, I will show you how to squash git commits into a single commit.

Goal

Consider that I have three commits below,

commit 9a0fb88031d65(HEAD -> master)

    Third commit

commit 598202a5e1ce0

    Second commit

commit 8bdd989b96368

    First commit


Now, I want to Squash both of them into a single commit i.e; I want to squash Third commit into Second commit. Use the command below,

git rebase -i HEAD~2

This will open up a shell window as shown below

pick 598202a Second commit
pick 9a0fb88 Third commit

and you need to replace pick with s as shown below and save and exit.

pick 598202a Second commit
s 9a0fb88 Third commit

This will open up a shell window as shown below,

# This is a combination of 2 commits.
# This is the 1st commit message:

Second commit

# This is the commit message #2:

Third commit

Just delete the squashed commit message and other text can also be deleted as below; save and exit,

Second commit

Finally, we have squashed Third commit into Second commit. Now, if you do git log you should see that the commit has been squashed as shown below,

commit aa1b27974570c85(HEAD -> master)

    Second commit

commit 8bdd989b963682a0

    First commit

Squashing more than two commits

If I need to do this for more than two commits into a single one, then increase the rebase count on HEAD, for instance, if you need to squash 3 commits into 1 then use command below,

git rebase -i HEAD~3

And while picking up you need to replace pick with s on both the commits which needs to be squashed. That’s it.

Checkout more tags @gitopscentral.in

git git

© Copyright 2016-2025 gitopscentral · All Rights Reserved ·