KatPadi's Point

Git: Squash Commits

With git it’s possible to squash previous commits into one before sharing them with others.

Step 1

For example you want to squash your last 3 commits:

git rebase -i HEAD~3

On the other hand, if you want to just simply squash all the unpushed commits:

git rebase -i origin/master

If you have many commits and want to start from a certain commit:

git rebase -i

Any of the command above will prompt open your editor with something like:

pick a92f09 Added new feature X
pick ca9f90a Some other stuff I did
pick d18a6h1 More stuff I did

This will show up in your editor:

pick a92f09 Added new feature X
squash ca9f90a Some other stuff I did
squash d18a6h1 More stuff I did

Note: If you don’t have an editor defined in your config, you will encounter Could not execute editor. Just do git config --global core.editor /usr/bin/vim for you to be able to proceed.

Step 2

Next, we can configure git on what to do with the commits. Let’s say, I want to keep commit a92f09. Git squash-ing the following two commits into the first one will leave us with one single commit with all the commits in it. To do that, change your file to this:

pick a92f09 Added new feature X
squash ca9f90a Some other stuff I did
squash d18a6h1 More stuff I did

Save and exit.

That’s it. Git squash is especially useful if you want to wrap up “all in a day’s work” or if you want to have a clean and concise git history.

TL;DR

Use git rebase -i origin/master and replace “pick” on the second and succeeding commits with “squash”.

git squash?

6 comments for “Git: Squash Commits

  1. March 2, 2017 at 1:26 pm

    Since we re combining so many commits, Git allows you to modify the new commit s message based on the rest of the commits involved in the process. Edit the message as you see fit, then save and quit. Once that s done, your commits have been successfully squashed!

  2. March 9, 2017 at 3:49 am

    And obviously I was squashing the last five commits on branch foo, but you could change that however you like.

  3. Doratisee
    April 6, 2017 at 4:27 am

    hi!

  4. December 24, 2021 at 5:56 am

    We are a group of volunteers and opening a new scheme in our community. Your web site offered us with valuable information to work on. You’ve done an impressive job and our entire community will be thankful to you.

  5. Beice
    December 24, 2021 at 9:08 am

    Great idea to write about such stuff here. I am grateful for every word.
    [url=https://ilxxxindex.simplesite.com/450916574]נערות ליווי בתל אביב[/url]

  6. pem
    December 29, 2021 at 6:02 pm

    Nothing to add, just I love to spend time in this website and read every word here.
    I this like as well:

    [url=https://www.eater.com/users/frank4865]נערות ליווי בבת ים[/url]

Leave a Reply to Nes Cancel reply

Your email address will not be published. Required fields are marked *