Git
Coding
Repository Management
GitHub
Commit Squashing

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

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?

  1. Simplify git history: When you squash commits, you're consolidating many commit entries into a few or one, which simplifies the git history.
  2. Ease code review process: A single commit is easier to review compared to multiple commits that might introduce changes incrementally.
  3. 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:

  1. Check out the branch you want to squash:
bash
   git checkout feature-branch
  1. Rebase interactively from the point the branch was created:
bash
   git rebase -i master

This command will open a text editor with a list of commits starting from the base branch (master in this case).

  1. Mark the commits to squash: In the text editor, you will see commits listed like this:
 
   pick e3a1b35 Initial commit
   pick 7ac9a67 Add new feature
   pick 1d2a3f4 Update configurations

To squash all commits into the first one, change the word pick to squash or s for all commits except the first one:

 
   pick e3a1b35 Initial commit
   squash 7ac9a67 Add new feature
   squash 1d2a3f4 Update configurations

Save and close the editor.

  1. 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.
  2. Force-push the squashed commits to the remote branch (if needed):
bash
   git push --force

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

ActionGit CommandPurpose
Check out branchgit checkout feature-branchMove to the branch where you want to squash commits.
Start interactive rebasegit rebase -i masterBegin process to interactively rebase from master.
Change ‘pick’ to ‘squash’Edit file in text editorMark commits to be combined into a single commit.
Combine commit messagesFollow prompts in text editorEdit the commit messages to reflect the squashed commit.
Force push the squashed commitgit push --forceUpdate 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
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.