codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
📦

Git & GitHub

49 / 63 topics
47Using BFG Repo-Cleaner and Filter-Repo48Rewriting Commit History49Squashing Commits50Using git amend for Commit Corrections
Tutorials/Git & GitHub/Squashing Commits
📦Git & GitHub

Squashing Commits

Updated 2026-04-20
4 min read

Squashing Commits

Introduction

In software development, commits are essential for tracking changes made to a project over time. However, as projects evolve, developers often end up with numerous small, incremental commits that can clutter the commit history. This is where squashing commits comes in handy. Squashing commits allows you to combine multiple commits into a single commit, making your repository's history cleaner and easier to understand.

In this tutorial, we'll explore how to squash commits using Git and GitHub. We'll cover both interactive rebase and GitHub's web interface for squashing commits. By the end of this guide, you'll be able to effectively manage your project's commit history.

Prerequisites

Before diving into squashing commits, ensure you have:

  • A basic understanding of Git and version control.
  • Git installed on your local machine.
  • A GitHub account with access to a repository where you can practice squashing commits.

Squashing Commits Using Interactive Rebase

Interactive rebase is a powerful tool that allows you to modify commit history. It's particularly useful for squashing multiple commits into one.

Step-by-Step Guide

  1. Open Your Terminal or Command Prompt

    Navigate to your local repository where you want to squash commits.

  2. Start the Interactive Rebase

    Use the following command to start an interactive rebase:

    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:

    git rebase -i HEAD~3
    
  3. Edit the Rebase Instructions

    A text editor will open displaying a list of the selected commits in reverse chronological order (from oldest to newest). Each line represents a commit and starts with the word pick. To squash commits, change pick to squash or s for all commits except the first one.

    For example:

    pick abc1234 Commit message 1
    squash def5678 Commit message 2
    squash ghi9012 Commit message 3
    

    This configuration will combine the last two commits into the first commit.

  4. Save and Close the Editor

    After editing, save the file and close the editor. Git will then prompt you to edit the combined commit message for all squashed commits.

  5. Edit the Combined Commit Message

    Open another text editor with the combined commit messages from the squashed commits. You can modify this message as needed, then save and close the editor.

  6. Complete the Rebase

    If there are no conflicts, Git will complete the rebase process. If there are conflicts, you'll need to resolve them manually before continuing the rebase.

  7. Force Push the Changes

    Since you've rewritten commit history, you'll need to force push the changes to your remote repository:

    git push origin <branch-name> --force
    

    Replace <branch-name> with the name of the branch you're working on.

Best Practices

  • Communicate with Your Team: Force pushing can overwrite history, so it's crucial to communicate with your team before doing so.
  • Use Interactive Rebase Sparingly: While interactive rebase is powerful, use it judiciously. It should be used primarily for cleaning up local branches or feature branches before merging into the main branch.

Squashing Commits Using GitHub

GitHub provides a user-friendly interface to squash commits directly from the web interface, which can be particularly useful for pull requests.

Step-by-Step Guide

  1. Navigate to Your Pull Request

    Go to your repository on GitHub and open the pull request where you want to squash commits.

  2. Click on "Commits" Tab

    Locate and click on the "Commits" tab in the pull request page.

  3. Squash and Merge Button

    At the bottom of the "Commits" tab, you'll see a "Squash and merge" button. Click on it to proceed.

  4. Edit Commit Message (Optional)

    You can edit the commit message for the squashed commits if needed. This is where you can combine multiple commit messages into one meaningful message.

  5. Confirm Merge

    Review your changes, then click the "Squash and merge" button again to complete the process.

Best Practices

  • Review Changes: Always review the changes before squashing and merging to ensure that all commits are included and correctly combined.
  • Communicate with Your Team: Inform your team about the squash operation so they're aware of the changes in the commit history.

Conclusion

Squashing commits is a valuable technique for maintaining clean and understandable commit histories. Whether you prefer using Git's interactive rebase or GitHub's web interface, both methods offer effective ways to manage your project's version control.

By following the steps outlined in this tutorial, you can now squash commits confidently, improving the maintainability and readability of your repository's history. Remember to use these tools judiciously and communicate with your team to avoid any disruptions caused by rewriting commit history.


PreviousRewriting Commit HistoryNext Using git amend for Commit Corrections

Recommended Gear

Rewriting Commit HistoryUsing git amend for Commit Corrections