Git
Version Control
Commit Management
Git Squash
Software Development

How do I squash my last N commits together?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Understanding Git Commits and Squashing

When working with Git version control, developers often create multiple commits to track various stages of development. However, there are situations where you might want to combine several commits into one comprehensive commit. This process is known as "squashing".

What is Squashing?

Squashing in Git refers to the process of combining multiple commits into a single, cohesive commit. This is often done to clean up the commit history, especially when you have made numerous small commits that logically represent a single change or feature.

Squashing is particularly useful in the following scenarios:

  • Simplifying history: Creating a cleaner commit history by combining minor fixes or work-in-progress commits.
  • Preparing for a pull request: Ensuring that the commit history on a feature branch is concise and meaningful before integrating with the main branch.
  • Reducing clutter: Avoid an unwieldy history filled with unnecessary commit messages.

Squashing Commits Using Interactive Rebase

One of the most preferred methods for squashing commits is using an interactive rebase. Here’s a step-by-step guide on how to squash the last N commits using this method.

Step-by-Step Guide

Step 1: Start an Interactive Rebase Session

To begin squashing commits, you’ll need to start an interactive rebase session. Open a terminal and navigate to your Git repository. Then execute the following command:

bash
git rebase -i HEAD~N

Replace N with the number of commits you want to squash. For example, if you want to squash the last 3 commits, use HEAD~3.

Step 2: Choose Squash for the Commits

After executing the command, an editor will open presenting a list of the last N commits:

plaintext
pick <commit-hash> Commit message #1
pick <commit-hash> Commit message #2
pick <commit-hash> Commit message #3

Change all lines starting from the second commit from pick to squash (or simply s). Here's how it should look:

plaintext
pick <commit-hash> Commit message #1
squash <commit-hash> Commit message #2
squash <commit-hash> Commit message #3

The pick option is retained for the commit you want to keep as the base, while the rest will be squashed into it.

Step 3: Edit the Commit Message

After closing the editor, Git will pause to let you combine commit messages. This is another editor window where you can edit the commit message:

plaintext
1# This is a combination of N commits.
2# The first commit message:
3
4Commit message #1
5
6# This is the 2nd commit message:
7
8Commit message #2
9
10# This is the 3rd commit message:
11
12Commit message #3

Edit this message to reflect the consolidation of these commits into a single meaningful commit message.

Step 4: Finalize the Rebase

Once you've edited and saved the commit message, Git will proceed to reapply the commits. If there are any conflicts, Git will prompt you to resolve them before continuing with the rebase. After resolving conflicts, use:

bash
git rebase --continue

Considerations

  • Amend with Caution: Squashing rewrites history, which if not used cautiously can cause issues, especially on shared branches.
  • Backup: It’s prudent to have a backup before performing any history rewrite operations.
  • Interactive Rebase: Selective and comprehensive, it offers more control over which commits you squash.

Alternatives to Interactive Rebase

While interactive rebase is a preferred method for many, Git also offers other ways to squash commits:

  • Merge: Temporarily merge the branch into a new one, then cherry-pick the resultant merge commit.
  • Reset and Commit: Reset to a previous commit and add changes to the current working directory in a fresh commit.

Summary Table

Here's a quick overview of the steps involved in squashing commits:

ActionCommand/Description
Start interactive rebasegit rebase -i HEAD&#126;N
Modify commit selectionsChange pick to squash (or s) for each commit to be combined
Edit commit messageModify the auto-generated message if needed and save the editor
Continue rebase (if needed)git rebase --continue
Resolve conflicts (if any)Manually solve conflicts, stage changes, and use git rebase --continue

By using squashing, you can ensure your commit history is clean, concise, and purposeful, making it easier for collaborators (and yourself) to understand the trajectory of changes in your project.


Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.