AlgoMaster Logo

Squashing Commits

Last Updated: January 3, 2026

6 min read

Squashing allows you to combine multiple commits into a single cohesive commit, significantly streamlining your project history. This is particularly useful for cleaning up the commit log before merging branches or when you want to create a more readable project history.

Understanding how to effectively squash commits not only simplifies your Git history but also helps in maintaining clarity and coherence in collaborative projects.

What is Commit Squashing?

At its core, commit squashing is the process of taking several commits and condensing them into a single commit. This can be particularly beneficial for:

  • Cleaning up commit history: Multiple small or "fix" commits can clutter the commit log.
  • Improving readability: A single commit that summarizes changes offers more context than a series of incremental updates.
  • Facilitating code review: A cleaner commit history makes it easier for reviewers to understand what changes were made without digging through numerous commits.

When you squash commits, the resulting commit combines the changes from each commit being squashed, and you can provide a single, descriptive commit message that summarizes the collective changes.

How to Squash Commits

Git provides a straightforward way to squash commits through interactive rebasing. Here, we'll outline the steps to perform this operation effectively.

Step-by-Step Process

To squash commits using interactive rebase, follow these steps:

1. Identify the Base Commit:

First, determine how many commits you want to squash. You can do this by running:

This command gives you a concise view of your commit history.

2. Start Interactive Rebase:

Once you've identified the base commit (the commit before the first commit you want to squash), initiate the interactive rebase. For example, to squash the last three commits, you would run:

3. Choose "squash" in the Editor:

After you run the above command, Git opens your default text editor, showing a list of commits. The first commit will be marked as "pick", while the others will be listed below it. To squash commits, change "pick" to "squash" (or simply "s") for the commits you want to combine:

4. Save and Close the Editor:

After making your changes, save the file and close the editor. Git will then combine the selected commits into the first one.

5. Edit Commit Message:

Finally, Git will prompt you to edit the commit message for the new squashed commit. You can choose to keep the existing messages or write a new one that summarizes the changes.

6. Complete the Rebase:

After saving your new commit message, the rebase completes, and your commit history is now cleaner.

Understanding the Squash Process

To fully appreciate commit squashing, it's essential to understand what happens to your Git history and the underlying object model during the process.

The Object Model

Each commit in Git is represented as an object that contains:

  • A snapshot of the project at that point in time (the tree object).
  • A set of pointers to the parent commits (commit object).
  • Metadata, including the author and commit message.

When you squash commits, you create a new commit object that references the same tree objects as the squashed commits but with a new commit message. The original commits remain in the repository's history until they are garbage collected.

Visualization of Squashing

Consider this simple scenario where we have three commits:

Here, commit C is the latest, and you want to squash it with commits A and B. After squashing, the history will look like this:

In this representation, commit D contains the combined changes of commits B and C, with a new message that reflects the overall changes.

Real-World Applications of Squashing

Squashing commits can be particularly useful in various scenarios:

Feature Branch Cleanup

When working on a feature branch, developers often make numerous small commits to track progress. Before merging this branch into the main branch, squashing these commits into a single one provides a clearer presentation of what the feature accomplishes.

For example, imagine you are working on a feature that adds user authentication. You might have several commits, such as:

  • Added user login UI
  • Fixed typo in login message
  • Updated styles for the login form
  • Implemented authentication logic

Squashing these into a single commit titled "Implement user authentication" offers a cleaner and more informative history.

Preparing for Code Reviews

Before submitting code for review, squashing commits can help reviewers focus on the essential changes instead of getting lost in incremental updates. A single commit often gives a better context for reviewing the functionality that has been added or modified.

Collaborative Projects

In team environments, squashing commits helps maintain a neat and organized project history. It reduces the noise from personal commits that may not be relevant to others and allows the team to focus on key features and bug fixes.

Nuances to Consider

While squashing commits is beneficial, it’s important to bear in mind certain nuances that can impact your workflow.

Preserving Commit History

Squashing can lead to a loss of detailed history. If your commits contain meaningful messages that describe incremental changes, you may want to retain those instead of squashing them all together. Always assess whether the details lost in squashing are important for future reference.

Conflicts During Rebase

If there are conflicts when squashing commits, Git will pause and ask you to resolve them. After resolving the conflicts, you need to mark them as resolved with:

Then continue the rebase with:

This process introduces additional steps, making it essential to be diligent when squashing commits with potential conflicts.

Interactive Rebase Limits

The interactive rebase interface can become unwieldy if you are squashing a large number of commits. In such cases, consider breaking down your squashing into smaller groups or using tools that can automate parts of the process.

Now that you understand how to squash commits and the benefits it brings to your Git workflow, you are ready to explore reordering commits.

In the next chapter, we will look at how to arrange your commits in a meaningful way to further enhance your project's history and coherence.