How to Squash All Commits on a Git Branch

Avatar

By squashlabs, Last Updated: Oct. 28, 2023

How to Squash All Commits on a Git Branch

To squash all commits on a Git branch, you can follow these steps:

Step 1: Checkout the branch

Before you can squash the commits, you need to make sure you are on the branch that you want to squash the commits on. You can use the following command to switch to the desired branch:

git checkout <branch-name>

Replace <branch-name> with the name of the branch you want to squash the commits on.

Related Article: How to View Your Global Git Configuration

Step 2: Rebase interactively

Once you are on the branch, you can use the interactive rebase feature of Git to squash the commits. This allows you to choose which commits to squash and how to combine them.

To start the interactive rebase, use the following command:

git rebase -i HEAD~<number-of-commits>

Replace <number-of-commits> with the number of commits you want to squash. For example, if you want to squash the last 3 commits, you would use HEAD~3.

Step 3: Squash the commits

After running the interactive rebase command, a text editor will open with a list of the commits you selected. Each commit will be marked with the word "pick". To squash a commit, change the word "pick" to "squash" or "s" for short.

For example, if you have the following commits:

pick abc123 Commit 1pick def456 Commit 2pick ghi789 Commit 3

Change it to:

pick abc123 Commit 1squash def456 Commit 2squash ghi789 Commit 3

Save the file and close the text editor to continue with the rebase.

Step 4: Edit the commit message (optional)

If you want to modify the commit message of the resulting squashed commit, Git will prompt you to do so after saving the file in the previous step. You can edit the commit message directly in the text editor that opens.

Once you are satisfied with the changes, save the file and close the text editor to complete the rebase.

Related Article: Fixing the Git Error: 'Fatal Not Possible To Fast Forward'

Step 5: Push the changes

After squashing the commits, you need to force push the changes to update the remote branch. Use the following command to push the changes:

git push --force

It's important to note that force pushing can overwrite existing commits on the remote branch, so use this command with caution.

Alternative Approach: Using Git Reset

Instead of using interactive rebase, you can also achieve the same result by using git reset and git commit. Here's an alternative approach:

1. Checkout the branch you want to squash the commits on:

git checkout <branch-name>

2. Reset the branch to the commit before the first commit you want to squash:

git reset --soft HEAD~<number-of-commits>

Replace <number-of-commits> with the number of commits you want to squash.

3. Commit the changes with a new commit message:

git commit -m "New commit message"

4. Push the changes to update the remote branch:

git push --force

This alternative approach achieves the same result as the interactive rebase method but uses a different set of Git commands.

Best Practices

Here are some best practices to consider when squashing commits on a Git branch:

- Squash commits with related changes: It's best to squash commits that contain related changes. This helps maintain a clean and logical commit history.

- Write meaningful commit messages: When squashing commits, take the opportunity to write a concise and descriptive commit message that accurately reflects the changes made.

- Review changes before squashing: Before squashing commits, it's a good practice to review the changes made in each commit to ensure that important changes are not accidentally lost.

- Communicate with your team: If you are working in a team, it's important to communicate with your team members before force pushing changes to a shared branch. This helps avoid conflicts and ensures everyone is aware of the changes being made.

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

How to Push Changes to a Remote Repository with Git Push

Learn how to use git push to send your local code changes to a remote repository. Understand remote repositories, set them up, and push changes effic… read more

How To Fix 'Updates Were Rejected' Error In Git

Software development has become more complex, and engineers face new challenges every day. Deploying and testing web applications can be particularly… read more

How To Push And Track A Local Branch In Git

Learn how to push a new local branch to a remote Git repository and track it for seamless collaboration. This step-by-step guide will walk you throug… read more

How To Modify Unpushed Commit Messages

Modifying unpushed commit messages in Git is a simple task using the git commit amend command. In this article, you will learn how to identify the co… read more

How To Use Git Remote Add Origin And Remote Set Url Origin

Adding a remote repository is an essential step in collaborating with other developers and pushing your code to a remote server. This article will gu… read more

How to Fully Delete a Git Repository Created With Init

Guide on the process of fully deleting a Git repository created using Init. This article provides step-by-step instructions on removing the local rep… read more

How to Fix Git Error: Could Not Read From Remote Repository

A simple guide to resolve the common Git error 'Could Not Read From Remote Repository.' Learn how to verify the remote repository URL, check authenti… read more

How to Download a Single Folder from a Github Repo

Downloading a single folder from a GitHub repository using Git can be done in two ways: using the GitHub website or using the Git command-line tool. … read more

How to Push a Tag to a Remote Repository Using Git

Pushing a tag to a remote repository using Git can be a process. This article provides a simple guide on how to accomplish this task with two methods… read more

How To Fix 'Could Not Open A Connection To Your Authentication Agent' In Git

Learn how to troubleshoot and resolve the 'Could Not Open a Connection to Your Authentication Agent' error in Git with simple steps. Discover possibl… read more