Git
Version Control
Merge
Branching
Software Development

How to squash all commits on branch

Interview Questions practice on Codemia

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

Browse interview questions

Squashing commits in Git is a powerful technique that helps you clean up your commit history, making it more readable and easier to understand. This is particularly useful when you're working on a feature branch and you want to merge it into the main branch with a single meaningful commit. In this article, we'll take a deep dive into the process of squashing commits, exploring the technical aspects and offering practical examples.

What is Squashing?

In Git, squashing refers to the process of combining multiple commits into a single one. This can be particularly useful in the following scenarios:

  1. Feature Completion: You've been working on a new feature and have made several commits during this development. Before merging this feature into the main branch, you want to condense these changes into a single commit that represents the entire feature.
  2. Clean-Up: You've made several "work-in-progress" or "fix typo" commits that you don't want cluttering up the project's history.

The Basics of Squashing Commits

The process of squashing commits relies on the git rebase command, which is used to reapply commits on top of another base tip. Using git rebase -i (interactive mode) allows you to combine multiple commits.

Step-by-Step Example

  1. Navigate to the Feature Branch: First, make sure you're on the feature branch where the commits you want to squash reside.
bash
   git checkout feature-branch
  1. Identify Commits: List the commits in the branch to determine how many you want to squash.
bash
   git log
  1. Start Interactive Rebase: Initiate an interactive rebase, specifying the parent of the oldest commit you want to squash:
bash
   git rebase -i HEAD~n

Replace n with the number of commits you wish to squash.

  1. Edit the Commit List: An editor will open containing a list of the selected commits. By default, they're marked with pick. Change all but the first commit from pick to squash or fixup:
    • squash: Combines the commit messages.
    • fixup: Uses the commit message of the first commit only.
plaintext
   pick e1e11f0 Initial commit for feature
   squash d2d22b7 Added helper functions
   squash a3a33c8 Refactored components
  1. Finalize the Rebase: After saving and closing the editor, Git will proceed with the rebase. If you used squash, another editor window will appear where you can edit the commit message of the combined commit. After editing, save and close the editor.
  2. Resolve any Conflicts: If there are merge conflicts, Git will pause and allow you to resolve them. After resolving, continue:
bash
   git add .
   git rebase --continue
  1. Push Changes: Once the rebase is complete, you'll need to force push the changes back to the remote branch, as the commit history has been rewritten:
bash
   git push origin feature-branch --force

Considerations for Squashing

  • Rewriting History: Squashing commits changes commit history. This is why git push --force is necessary. Be cautious about rewriting history in shared branches, as it can confuse collaborators.
  • Collaborative Environments: Before squashing, communicate with your team. If others are working on the same branch, they will need to accommodate the rewritten history.

Table: Choices During Squashing Commits

ActionDescription
pickUse commit as-is.
editEdit the commit before continuing.
squashCombine the commit with the previous one.
fixupLike squash, but discard commit message.
dropRemove the commit entirely.

Advanced Squashing Techniques

  1. Interactive Squash During Merge: You can also squash commits while merging branches using the --squash option. This applies all the changes from a branch but doesn't keep its history.
bash
   git merge --squash feature-branch
  1. Squashing Post Merge: If a branch is already merged, you can still squash the subsequent commits using rebase, but it can be more complex and often requires extensive conflict resolution.

Conclusion

Squashing commits is a valuable practice for maintaining a clean, informative commit history. While powerful, it's a technique that should be used judiciously, particularly when collaborating in shared environments. Understanding the rebase process and being able to resolve conflicts are essential skills for effectively squashing commits. By mastering these concepts, you can greatly improve the quality and readability of your project's history.


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.