git
version control
squash commits
merge branches
GitHub

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 master or main typically 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

  1. 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:
bash
   git checkout feature-branch
  1. Interactive Rebase Use interactive rebase to squash the commits. Assume you have n commits you want to squash:
bash
   git rebase -i HEAD~n

This command opens an editor with the last n commits listed.

  1. Choose Commits to Squash
    • In the editor that opens, you'll see the list of commits like:
 
     pick f1e2d3f Commit 1
     pick d4e5f6a Commit 2
     pick a1b2c3d Commit 3
  • Change all but the first pick to squash (or just s) like below:
 
     pick f1e2d3f Commit 1
     squash d4e5f6a Commit 2
     squash a1b2c3d Commit 3
  1. 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.
  2. 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.
  3. Switch to the Target Branch Move to the branch where you plan to merge the squashed commit:
bash
   git checkout target-branch
  1. Merge the Squashed Commit With the squashed changes finalized on your feature branch, merge it into your target branch:
bash
   git merge --no-ff feature-branch
  1. Push Changes to Remote Ensure your local changes are reflected on the remote repository:
bash
   git push origin target-branch

Table: Squash Commit Workflow Summary

StepDescription
1Checkout to the branch with commits to squash.
2Start interactive rebase with HEAD~n.
3Change pick to squash in desired commits.
4Reword or combine commit messages.
5Finalize the rebase and complete squashing.
6Switch to the target branch for merging.
7Merge squashed commits into the target branch.
8Push 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.


Course illustration
Course illustration

All Rights Reserved.