How to Merge Multiple Commits as Single Squashed Commit


By squashlabs, Last Updated: October 27, 2023

How to Merge Multiple Commits as Single Squashed Commit

To merge multiple commits as a single squashed commit in Git, you can follow these steps:

Step 1: Create a New Branch

Before you start squashing commits, it is a good practice to create a new branch to perform the squashing. This helps you keep the original branch intact and provides a clean history for the squashed commit. You can create a new branch using the following command:

$ git checkout -b <new-branch-name> <starting-commit>

Replace <new-branch-name> with the desired name for your new branch and <starting-commit> with the commit from which you want to start squashing.

Step 2: Squash Commits

Once you have created a new branch, you can start squashing the commits into a single commit. Git provides several ways to achieve this, but the most common approach is to use the interactive rebase feature. Run the following command to initiate an interactive rebase:

$ git rebase -i <commit-to-squash>

Replace <commit-to-squash> with the commit hash or the commit reference that you want to squash the subsequent commits into. This command will open up an interactive editor with a list of commits.

Step 3: Edit the Commits

In the interactive editor, you will see a list of commits with their respective pick commands. To squash commits, you need to replace the pick command with the squash or s command for the commits that you want to squash. For example:

pick 1234567 Commit A
squash 890abcd Commit B
squash 1234abc Commit C

In the above example, commits B and C are being squashed into commit A. After making the necessary changes, save and close the editor.

A better way to build and deploy Web Apps

  Cloud Dev Environments
  Test/QA enviroments

One-click preview environments for each branch of code.

Step 4: Amend the Commit Message

Once you have specified the commits to squash, Git will prompt you to provide a new commit message for the squashed commit. By default, Git will concatenate the commit messages of the squashed commits. However, you can modify the commit message as per your requirements. Save and close the editor to finalize the squashed commit.

Step 5: Push the Squashed Commit

After squashing the commits, you will have a new squashed commit in your branch. You can now push this commit to the remote repository using the following command:

$ git push origin <new-branch-name>

Replace <new-branch-name> with the name of the branch you created in Step 1.

Alternative Approach: Merging with –squash

Another way to achieve the same result is by using the --squash option during the merge operation. This approach allows you to merge multiple commits from a feature branch into the main branch as a single squashed commit. Here’s how you can do it:

1. Checkout the branch where you want to merge the commits:

   $ git checkout <target-branch>

2. Merge the commits from the feature branch using the --squash option:

   $ git merge --squash <feature-branch>

Replace <feature-branch> with the name of the branch that contains the commits you want to squash.

3. Commit the changes with an appropriate commit message:

   $ git commit -m "Merge feature branch with squashed commits"

4. Push the squashed commit to the remote repository:

   $ git push origin <target-branch>

Replace <target-branch> with the name of the branch where you merged the squashed commit.

Best Practices

Here are some best practices to consider when squashing multiple commits into a single squashed commit:

– Squash only logically related commits: It is recommended to squash commits that are related to a single task or feature. This helps in maintaining a clean and concise commit history.

– Write meaningful commit messages: When squashing commits, take the opportunity to write a descriptive commit message that clearly explains the changes made in the squashed commit.

– Review the changes before squashing: Before finalizing the squashed commit, it is a good practice to review the changes made in each commit to ensure that nothing important is missed.

– Communicate with your team: If you are working in a team, it is important to communicate with your team members before squashing commits. Ensure that everyone is aware of the changes and agrees with the decision to squash the commits.

More Articles from the Git Tutorial: From Basics to Advanced Concepts series: