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.
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:
- 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.
- 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
- Navigate to the Feature Branch: First, make sure you're on the feature branch where the commits you want to squash reside.
- Identify Commits: List the commits in the branch to determine how many you want to squash.
- Start Interactive Rebase: Initiate an interactive rebase, specifying the parent of the oldest commit you want to squash:
Replace n with the number of commits you wish to squash.
- 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 frompicktosquashorfixup:squash: Combines the commit messages.fixup: Uses the commit message of the first commit only.
- 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. - Resolve any Conflicts: If there are merge conflicts, Git will pause and allow you to resolve them. After resolving, continue:
- 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:
Considerations for Squashing
- Rewriting History: Squashing commits changes commit history. This is why
git push --forceis 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
| Action | Description |
pick | Use commit as-is. |
edit | Edit the commit before continuing. |
squash | Combine the commit with the previous one. |
fixup | Like squash, but discard commit message. |
drop | Remove the commit entirely. |
Advanced Squashing Techniques
- Interactive Squash During Merge: You can also squash commits while merging branches using the
--squashoption. This applies all the changes from a branch but doesn't keep its history.
- 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
- How to squash all git commits into one?
- How to squash commits in git after they have been pushed?
- How to squash commits in git after they have been pushed?
- How to stash changes while keeping the changes in the working directory? Git
- How to stash only staged changes in Git?
- How to stash only staged changes in Git?
- How to stash only unstaged changes in Git?
- How to state in requirements.txt a direct github source
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack 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.