How to Merge Multiple Commits as Single Squashed Commit

Avatar

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.

Related Article: How to Undo a Git Rebase: A Tutorial

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.

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.

Related Article: How to Fix a Git Detached Head

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:

How to Remove a File From the Latest Git Commit

Learn how to remove a file from the latest Git commit using a simple process. Discover two approaches: interactive rebase and amending the commit. Explore best practices... read more

How to Cherry Pick Multiple Commits in Git

Cherry picking multiple commits in Git can be a useful technique when you only want to apply specific changes to your codebase. This article provides a step-by-step... read more

How To Cherry Pick A Commit With Git

Cherry picking a commit with Git allows you to selectively apply changes to your codebase. In this article, you will learn the meaning and process of cherry picking, as... read more

How To Combine My Last N Commits In Git

Learn how to combine multiple commits into a single one using Git's squash feature. Discover the potential reasons for combining commits and explore different methods to... read more

How To Change the Git Remote URL

Changing the Git remote URL for a repository is a simple process that can be done with a few easy steps. This article will guide you through the process, from checking... read more

How To Uncommit Last Git Commit

Learn how to uncommit your last commit in Git with simple steps and avoid unnecessary changes in your codebase. Find out two methods to uncommit, understand the reasons... read more