Git Squash Commits – Squashing the Last N Commits into One Commit

Posted: | Last updated: | 5 minute read

In the world of software development, Git is a powerful tool that helps teams manage changes to their codebase efficiently. One feature of Git that’s particularly useful for keeping a clean and organized history is squashing commits. Git squash allows you to combine multiple commits into a single, more cohesive commit. In this blog post, we’ll explore the concept of Git squash commits, why you might want to use them, and how to squash the last N commits into one commit with examples.

What is Git Squash?

Git squash is a technique used to condense multiple commits into a single commit. Instead of having several individual commits with incremental changes, squashing allows you to combine them into one commit with a comprehensive message. This can make your commit history cleaner and easier to understand, especially before merging changes into a main branch or sharing code with others.

Why Squash Commits?

There are several reasons why you might want to squash commits in Git:

Cleaner History: Squashing commits helps maintain a tidy commit history, making it easier to review changes and understand the evolution of the codebase.

Rebasing: Squashing commits is often done in conjunction with rebasing, a technique used to incorporate changes from one branch into another. Squashing commits before rebasing can streamline the process and reduce clutter.

Code Reviews: A single, well-crafted commit is easier to review than multiple smaller commits. Squashing commits can improve the efficiency of code reviews and make it easier for team members to provide feedback.

Simplifying Collaboration: When collaborating with others on a project, squashing commits can make it easier to share changes and ensure that each commit represents a meaningful contribution to the codebase.

How to Squash Commits in Git

Now, let’s walk through the process of squashing the last N commits into one commit using Git. Here’s a step-by-step guide:

Check Commit History: Start by reviewing the commit history to identify the commits you want to squash. You can use the git log command to view a list of recent commits.

Determine Number of Commits: Decide how many of the most recent commits you want to squash together. Let’s say you want to squash the last 3 commits into one.

Initiate Interactive Rebase: Use the git rebase -i HEAD~N command to initiate an interactive rebase, where N is the number of commits you want to squash. For example:

git rebase -i HEAD~3

Squash Commits: In the interactive rebase window that opens, you’ll see a list of the last N commits. Change the command next to the commits you want to squash from “pick” to “squash” or “s” for short.

Edit Commit Message: After marking the commits for squashing, Git will prompt you to edit the commit message for the new, combined commit. Modify the message as needed to summarize the changes included in the squashed commits.

Save Changes: Save and close the interactive rebase window once you’re satisfied with the changes. Git will then apply the squash and update the commit history accordingly.

Squashing Last 3 Commits

Let’s illustrate the process with an example. Suppose we have the following commit history:

Commit 1: Add feature A
Commit 2: Fix bug in feature A
Commit 3: Update documentation for feature A

We want to squash these three commits into a single commit with the message “Implement feature A.” Here’s how we would do it:

  1. Initiate an interactive rebase:
git rebase -i HEAD~3
  1. In the interactive rebase window, change the commands next to commits 2 and 3 from “pick” to “squash” or “s”.

  2. Save and close the interactive rebase window.

  3. Edit the commit message for the new, combined commit to “Implement feature A.”

  4. Save the changes, and Git will apply the squash, resulting in a single commit with the updated message.

Git squash commits offer a convenient way to streamline your commit history and present changes in a more cohesive manner. By squashing multiple commits into one, you can keep your repository clean, simplify collaboration, and improve the efficiency of code reviews. Whether you’re working on a personal project or collaborating with a team, mastering the art of squashing commits in Git is a valuable skill that can enhance your development workflow.

So, the next time you find yourself with a series of incremental commits, consider squashing them into a single, meaningful commit. Your future self and your collaborators will thank you for it!

FAQs

  1. What happens to the commit messages when squashing commits?
    When you squash commits, Git combines the commit messages of the squashed commits into a single commit message. You have the opportunity to edit this message during the interactive rebase process.

  2. Can I squash commits across different branches?
    Yes, you can squash commits across different branches using the interactive rebase feature in Git. However, keep in mind that rebasing and squashing commits can rewrite history, so use caution when doing this on shared branches.

  3. Will squashing commits affect my pull request or merge request?
    Squashing commits before merging changes into a main branch or creating a pull request can help maintain a clean and organized history. It’s a common practice to squash commits before merging to simplify the review process and ensure that each commit represents a meaningful change.

  4. Can I undo a squash operation if I make a mistake?
    If you make a mistake while squashing commits, you can abort the interactive rebase process by closing the rebase window without saving changes. This will revert back to the original commit history before the squash operation.

  5. Are there any limitations to squashing commits?
    While squashing commits can be beneficial for cleaning up a commit history, it’s important to use it judiciously. Avoid squashing commits that have already been shared with others or are part of a larger collaborative effort, as this can disrupt workflow and cause confusion.

  6. Can I squash commits with conflicts?
    Yes, you can squash commits that have conflicts during the interactive rebase process. Git will pause the rebase process and prompt you to resolve conflicts before continuing with the squash operation.

These FAQs should provide clarity on common questions related to Git squash commits and help you navigate the process with confidence. If you have any further questions, feel free to ask!