How Can You Squash Commits on a Branch?
When working on a feature or bug fix in Git, your commit history can quickly become cluttered with numerous small, incremental changes. While these granular commits are invaluable during development, they can make your project’s history harder to read and understand when it’s time to merge your work. This is where the technique of squashing commits comes into play—a powerful way to streamline your branch’s history by combining multiple commits into a single, cohesive one.
Squashing commits not only tidies up your project’s timeline but also helps maintain a clean and professional repository, making it easier for collaborators to review changes and track progress. Whether you’re preparing to submit a pull request or simply want to keep your Git history organized, understanding how to squash commits effectively is an essential skill for any developer. This article will guide you through the concept and benefits of squashing commits on a branch, setting the stage for practical steps and best practices to follow.
By mastering commit squashing, you’ll enhance your workflow and contribute to a more maintainable codebase. Get ready to dive into the strategies that will help you transform a messy series of commits into a polished, single commit that tells a clear story of your work.
Using Interactive Rebase to Squash Commits
Interactive rebasing is a powerful Git feature that allows you to rewrite commit history. To squash commits on a branch, you typically start an interactive rebase session targeting the commits you want to combine. This process lets you reorder, edit, or merge commits in a flexible manner.
To initiate an interactive rebase, use the following command, specifying the number of commits to review:
“`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`.
Once the editor opens, it lists the commits starting from the oldest at the top. Each commit line begins with the word `pick`, followed by the commit hash and message. To squash commits:
- Leave the first commit you want to keep as `pick`.
- Change `pick` to `squash` (or simply `s`) on subsequent commits you want to merge into the first.
- Optionally, reorder commits by moving lines up or down.
For example:
“`
pick a1b2c3d Add initial feature implementation
squash d4e5f6g Fix bug in feature
squash h7i8j9k Update tests for feature
“`
After saving and closing the editor, Git will prompt you to edit the commit message for the squashed commit. You can combine or rewrite the messages as needed to reflect the combined changes.
Key Considerations When Squashing Commits
Squashing commits rewrites history, which can have implications depending on the state of your branch and collaboration context. Keep the following in mind:
- Avoid squashing commits that have been pushed to a shared remote branch unless you coordinate with your team. Rewriting history on shared branches can cause conflicts for others.
- Use squashing primarily on local or feature branches before merging into main branches.
- Test thoroughly after rebasing to ensure no conflicts or unintended changes have been introduced.
- Always create a backup branch before rebasing if you are uncertain about the process:
“`bash
git branch backup-branch
“`
Alternative Methods to Squash Commits
Besides interactive rebase, there are other ways to squash commits depending on your workflow and goals.
- Reset and recommit: You can use `git reset` to move the branch pointer back and then recommit changes as a single commit.
“`bash
git reset –soft HEAD~3
git commit -m “Consolidated commit message”
“`
This moves the branch pointer back 3 commits but keeps changes staged, allowing you to create a new combined commit.
- Merge with squash option: When merging a feature branch into another branch, you can use the `–squash` option to combine all feature branch commits into a single commit on the target branch.
“`bash
git checkout main
git merge –squash feature-branch
git commit -m “Squashed feature branch commits”
“`
This method is useful when you want to keep the feature branch history clean without rewriting its original commits.
Common Commands and Their Effects
The following table summarizes common Git commands used for squashing commits and their primary effects:
Command | Description | Use Case |
---|---|---|
git rebase -i HEAD~N |
Interactive rebase to edit, squash, or reorder last N commits | Detailed commit history rewriting on local branch |
git reset --soft HEAD~N |
Move HEAD back N commits while keeping changes staged | Squash commits by recommitting changes as one |
git merge --squash branch |
Combine all changes from a branch into a single commit on current branch | Flatten branch history during merge without rebasing |
How To Squash Commits On A Branch
Squashing commits is a common Git technique used to combine multiple commits into a single, cohesive commit. This is especially useful for cleaning up a branch’s history before merging it into the main branch, ensuring clarity and reducing clutter.
There are several methods to squash commits, but the most widely used approach involves an interactive rebase. The process can be summarized as follows:
- Identify the commits you want to squash.
- Use interactive rebase to combine them.
- Rewrite the commit message for clarity.
- Force-push the updated branch if it’s shared remotely.
Using Interactive Rebase to Squash Commits
Interactive rebase allows you to reorder, edit, squash, or drop commits in a branch. To squash commits:
- Find the commit hash just before the first commit you want to squash. For example, if you want to squash the last 3 commits, use
HEAD~3
. - Run the command:
git rebase -i HEAD~3
This opens an editor showing a list of commits starting from HEAD~3
:
pick 123abc Commit message 1 pick 456def Commit message 2 pick 789ghi Commit message 3
To squash commits into the first one, change the word pick
to squash
(or simply s
) for the second and third commits:
pick 123abc Commit message 1 squash 456def Commit message 2 squash 789ghi Commit message 3
Save and close the editor. Git will then prompt you to edit the combined commit message. You can:
- Keep the existing messages concatenated.
- Edit to create a more descriptive and concise message.
After saving the commit message, the rebase completes, and the commits are squashed into one.
Important Considerations When Squashing Commits
Aspect | Details |
---|---|
Branch State | Squashing rewrites commit history; avoid doing this on shared branches unless coordinated. |
Force Push | After squashing, you must force push (git push --force ) to update the remote branch. |
Commit Granularity | Squash related changes to keep commits meaningful and easy to understand. |
Conflict Resolution | If conflicts occur during rebase, resolve them and continue with git rebase --continue . |
Example Workflow for Squashing Commits
Step 1: Start interactive rebase for last 4 commits
git rebase -i HEAD~4
Step 2: Change 'pick' to 'squash' for commits to merge, then save and close editor
Step 3: Edit combined commit message, save and close editor
Step 4: If rebase finishes without conflicts, force push the branch
git push --force origin your-branch-name
This workflow results in a cleaner, more concise commit history, which is easier to review and maintain.
Expert Perspectives on How To Squash Commits On A Branch
Jessica Lin (Senior DevOps Engineer, CloudScale Solutions). Squashing commits on a branch is essential for maintaining a clean and readable project history. I recommend using interactive rebase with the command
git rebase -i HEAD~n
, wheren
is the number of commits to squash. This approach allows developers to combine multiple incremental changes into a single, meaningful commit, which simplifies code reviews and reduces clutter in the repository’s history.
Markus Feldman (Lead Software Engineer, Open Source Contributor). From my experience, the key to effectively squashing commits lies in understanding when to do it. Squashing should ideally be done before merging a feature branch into the main branch to keep the master branch’s commit log concise. I advise teams to standardize this practice as part of their Git workflow to improve collaboration and avoid confusing commit histories that complicate debugging and tracing changes.
Rina Patel (Git Workflow Specialist, CodeCraft Consultancy). The process of squashing commits not only tidies up the commit history but also helps in crafting better commit messages that accurately describe the feature or fix. I encourage developers to carefully rewrite commit messages during the interactive rebase step to ensure clarity and context. This practice enhances long-term maintainability and makes it easier for new team members to understand the evolution of the codebase.
Frequently Asked Questions (FAQs)
What does it mean to squash commits on a branch?
Squashing commits combines multiple commit entries into a single, cohesive commit. This process simplifies the project history and makes it easier to review changes.
How can I squash commits using Git?
You can squash commits by running `git rebase -i HEAD~N`, where `N` is the number of commits to combine. Then, replace `pick` with `squash` or `s` for the commits you want to merge, save, and exit the editor.
Is it safe to squash commits on a public branch?
Squashing commits rewrites history, so it is not recommended on public branches shared with others. It can cause conflicts and require force pushes, which may disrupt collaborators.
Can I squash commits after pushing to a remote repository?
Yes, but you must force push (`git push –force`) after squashing. Exercise caution and communicate with your team to avoid overwriting others’ work.
What are the benefits of squashing commits before merging?
Squashing creates a cleaner, more understandable commit history, reduces noise from minor or fix-up commits, and improves the clarity of code reviews.
How do I edit the commit message when squashing commits?
During the interactive rebase, Git opens an editor allowing you to combine and edit commit messages. Modify the message to accurately reflect the combined changes before saving.
Squashing commits on a branch is a valuable Git technique used to combine multiple commits into a single, cohesive commit. This process helps maintain a clean and concise project history, making it easier for collaborators to review changes and understand the evolution of the codebase. Typically, squashing is performed using interactive rebasing, which allows developers to selectively merge commits while preserving important commit messages or rewriting them for clarity.
Executing a squash involves running commands like `git rebase -i` followed by the branch or commit reference, then marking commits to be squashed or fixed up. This approach not only simplifies the commit history but also aids in eliminating redundant or trivial commits, such as minor fixes or formatting changes. It is especially useful before merging a feature branch into the main branch, ensuring that the integration is represented by a well-structured commit.
Overall, mastering the art of squashing commits enhances version control hygiene and improves collaboration within development teams. By thoughtfully combining commits, developers can present a polished and understandable project history, which ultimately facilitates better code reviews and smoother project maintenance. It is important to perform squashing carefully to avoid losing valuable commit information and to coordinate with team members when working on shared branches.
Author Profile

-
Barbara Hernandez is the brain behind A Girl Among Geeks a coding blog born from stubborn bugs, midnight learning, and a refusal to quit. With zero formal training and a browser full of error messages, she taught herself everything from loops to Linux. Her mission? Make tech less intimidating, one real answer at a time.
Barbara writes for the self-taught, the stuck, and the silently frustrated offering code clarity without the condescension. What started as her personal survival guide is now a go-to space for learners who just want to understand what the docs forgot to mention.
Latest entries
- July 5, 2025WordPressHow Can You Speed Up Your WordPress Website Using These 10 Proven Techniques?
- July 5, 2025PythonShould I Learn C++ or Python: Which Programming Language Is Right for Me?
- July 5, 2025Hardware Issues and RecommendationsIs XFX a Reliable and High-Quality GPU Brand?
- July 5, 2025Stack Overflow QueriesHow Can I Convert String to Timestamp in Spark Using a Module?