Last modified: June 11, 2024

This article is written in: 🇺🇸

Git Squashing

In the world of Git, the iterative development process often results in multiple commits for minor changes. But before merging changes to a primary branch, it's valuable to have a clean, linear history. This is where the concept of "squashing" steps in.

What is Squashing?

At its core, squashing is about compressing several commits into one. This not only provides a concise view of the work done but also allows for a more streamlined commit history. While squashing can make the commit history neater, it is crucial to remember that squashing rewrites history, which can be problematic in shared branches.

Before Squashing:
-----------------

      master
        |
  A--B--C
        |
     feature
        |
  D--E--F--G
        |
     topic

After Squashing:
----------------

      master
        |
  A--B--C
        |
     feature
        |
        H
        |
     topic

Where:
D--E--F--G are combined into a single commit H.

Squashing vs. Merging

The distinction between merging and squashing is vital:

How to Squash the last N Commits

When it comes to squashing, the interactive rebase tool is your best friend:

  1. Using Rebase:

git rebase -i HEAD~N

After this command, a text editor will open showing the last N commits. Simply change the word 'pick' to 'squash' (or 's' for short) for every commit you want to squash into the previous one. Save and close the editor, and Git will squash the specified commits.

  1. Alternative using Merge: If you're not a fan of rebasing, you can achieve a similar result using reset and merge:

git reset --hard HEAD~N
git merge --squash HEAD@{1}
git commit

  1. Force Pushing: Sometimes, after squashing locally, you need to update a remote branch. Do this with caution, as rewriting shared history can be disruptive to other collaborators:

git push origin branch_name --force

A Word of Caution

Squashing, especially with force pushing, can be dangerous in collaborative environments. It's crucial to communicate with your team and ensure everyone understands the changes made to the shared branch's history.

Table of Contents

    Git Squashing
    1. What is Squashing?
    2. Squashing vs. Merging
    3. How to Squash the last N Commits
    4. A Word of Caution