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.
Git is a version control system that allows multiple developers to work on a project without interfering with each other's work. Branching in Git enables you to diverge from the main line of development and continue to work independently without affecting the main line. Sometimes, especially in the context of cleaning up a branch or preparing for a merge, you might want to combine all the commits on your branch into a single commit. This process is known as squashing commits. Squashing makes your history cleaner and easier to understand.
Why Squash Commits?
- Simplify git history: When you squash commits, you're consolidating many commit entries into a few or one, which simplifies the git history.
- Ease code review process: A single commit is easier to review compared to multiple commits that might introduce changes incrementally.
- Preparation for merging: Squashing commits is often done before merging a feature branch back into the main branch, making the merge process smoother and maintaining a clean history on the main branch.
How to Squash Commits in Git
The most common method to squash all commits on a branch is using the git rebase command interactively (-i). This method allows you to select and combine commits that you want to squash.
Step-by-step Guide
Suppose you are working on a feature branch called feature-branch. Here’s how you can squash all commits since its divergence from the main branch:
- Check out the branch you want to squash:
- Rebase interactively from the point the branch was created:
This command will open a text editor with a list of commits starting from the base branch (master in this case).
- Mark the commits to squash: In the text editor, you will see commits listed like this:
To squash all commits into the first one, change the word pick to squash or s for all commits except the first one:
Save and close the editor.
- Combine commit messages: Git will prompt you to modify commit messages. This is your chance to create a single commit message that describes the entire feature or set of changes.
- Force-push the squashed commits to the remote branch (if needed):
Important Considerations
- Back up your branch before rebasing. Rebasing, especially squashing, alters the history of your branch. It's a good safety measure to create a backup branch just in case something goes wrong.
- Never squash commits on a public/shared branch. Others may have based work on this branch, and changing its history can cause significant problems for collaborative development.
Summary Table
| Action | Git Command | Purpose |
| Check out branch | git checkout feature-branch | Move to the branch where you want to squash commits. |
| Start interactive rebase | git rebase -i master | Begin process to interactively rebase from master. |
| Change ‘pick’ to ‘squash’ | Edit file in text editor | Mark commits to be combined into a single commit. |
| Combine commit messages | Follow prompts in text editor | Edit the commit messages to reflect the squashed commit. |
| Force push the squashed commit | git push --force | Update the remote repository with the new history. |
Additional Details
Handling Conflicts: During the rebase, you might encounter conflicts. Git will stop at the conflicting commit and allow you to fix the conflict. Once resolved, you should continue the rebase process with git rebase --continue.
By carefully following the steps and considering the impacts, squashing commits can greatly improve your project’s history clarity and management.
Related reading
- How to squash all commits on branch
- 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?
.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.