How can I merge multiple commits onto another branch as a single squashed commit?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Merging multiple commits onto another branch as a single squashed commit is a common practice in Git workflows, especially when you want to maintain a clean and concise history. Squashing is an effective way to condense numerous commit activities into a single commit message on the target branch, making it easier to review changes and track development progress.
Understanding Git Commits and Branches
Before diving into squashing, it's essential to understand how Git manages commits and branches:
- Commit: A snapshot of your code at a particular state. Each commit has a unique SHA-1 hash, author information, commit message, and parent information.
- Branch: A pointer to a sequence of commits. Branches like
masterormaintypically represent stable code, while feature branches might contain isolated, in-progress work.
The Squash Process
Squashing commits involves combining multiple commits into a single commit. This process can be useful when:
- You want to merge a feature branch into a main branch and prefer a single commit that represents the entire feature.
- You need to clean up commit history by consolidating multiple minor or fix-up commits.
Prerequisites
Before proceeding, ensure you have:
- Git installed.
- A branch containing multiple commits you want to squash.
- A target branch where the squashed commit needs to be applied.
Step-by-Step Guide
- Checkout to the Branch with Commits to Squash Begin by switching to the branch that contains the commits you wish to squash. Use the command:
- Interactive Rebase Use interactive rebase to squash the commits. Assume you have
ncommits you want to squash:
This command opens an editor with the last n commits listed.
- Choose Commits to Squash
- In the editor that opens, you'll see the list of commits like:
- Change all but the first
picktosquash(or justs) like below:
- Combine Commit Messages After closing the editor, Git will take you to another editing screen to combine commit messages. Write a single commit message representing the squashed commit or clean up the existing messages.
- Finalize the Rebase Save and close the editor to complete the rebase process. Git will squash the specified commits into a single commit with the combined message.
- Switch to the Target Branch Move to the branch where you plan to merge the squashed commit:
- Merge the Squashed Commit With the squashed changes finalized on your feature branch, merge it into your target branch:
- Push Changes to Remote Ensure your local changes are reflected on the remote repository:
Table: Squash Commit Workflow Summary
| Step | Description |
| 1 | Checkout to the branch with commits to squash. |
| 2 | Start interactive rebase with HEAD~n. |
| 3 | Change pick to squash in desired commits. |
| 4 | Reword or combine commit messages. |
| 5 | Finalize the rebase and complete squashing. |
| 6 | Switch to the target branch for merging. |
| 7 | Merge squashed commits into the target branch. |
| 8 | Push final changes to the remote repository. |
Additional Considerations
- Backup: Always back up important branches before performing operations like rebasing, as they rewrite commit history and can be potentially destructive if not handled correctly.
- Collaboration: If working in a team, inform others when rebasing a shared branch to prevent conflicts and data loss.
- Git Aliases: Consider creating Git aliases for frequently used commands to simplify your workflow.
Utilizing command-line Git tools efficiently allows developers to manage code history cleanly, making later development and collaboration efforts smoother. Understanding how to squash commits skillfully is an empowering tool in a developer's arsenal.

