How to Squash All Commits on a Git Branch

Avatar

By squashlabs, Last Updated: October 27, 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 Make Git Stop Tracking a File in .Gitignore

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 1
pick def456 Commit 2
pick ghi789 Commit 3

Change it to:

pick abc123 Commit 1
squash def456 Commit 2
squash 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: How to Fully Delete a Git Repository Created With Init

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 Undo a Git Merge That Hasn’t Been Pushed Yet

A simple guide to undo a Git merge before pushing it. Learn two methods using Git Reset and Git Revert commands, along with additional... read more

How to Throw Away Local Commits in Git

Removing local commits in Git can be a simple process with the right methods. This article provides two methods, using git reset and git revert, to help you throw away... read more

How To Delete A Commit From A Branch

Deleting a commit from a branch in Git can be done using simple steps and commands. There are two methods you can use: git revert and git reset. But why would you want... read more

How To Revert A Git Repo To a Previous Commit

Learn how to revert a Git repository to a previous commit using the git reset command. This article discusses two methods, git reset and git revert, to easily revert a... read more