How to Squash All Commits on Branch in Git

Git is a popular version control system used by software developers to manage their code.

It allows multiple people to work on the same codebase, track changes, and resolve conflicts.

One of the features of Git is the ability to squash multiple commits into a single commit.

This is particularly useful when you want to clean up a branch before merging it into the main branch.

In this tutorial, we’ll explore how to squash all commits into a single commit in Git.


Why Squash Commits?

Squashing commits is a technique used to reduce the number of commits in a branch.

This helps to simplify the commit history, making it easier to read and understand.

By squashing multiple commits into a single commit, you can create a cleaner, more concise history of changes to the code.

Squashing commits can also help to resolve conflicts during a merge.

When multiple people work on the same branch, each person may create multiple commits.

If each person’s changes are squashed into a single commit, it becomes easier to resolve any conflicts that may arise during the merge process.

How to Squash Commits in Git

Squashing commits in Git can be done using the command line or through a graphical user interface (GUI) such as GitKraken or SourceTree.

In this section, we’ll show you how to squash commits using the command line.

Check the Commit History

Before you squash commits, it’s important to check the commit history to see what you’re working with.

To do this, run the following command in the terminal:

$ git log

This will display the commit history, including the commit hash, author, date, and commit message.

Rebase the Branch

To squash commits, we’ll use the rebase command.

The rebase command allows you to reapply a series of commits to the current branch.

To rebase a branch, run the following command in the terminal:

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

Where <number of commits> is the number of commits you want to squash.

For example, if you want to squash the last 3 commits, you would run the following command:

$ git rebase -i HEAD~3

Edit the Commit List

When you run the rebase command, Git will open an editor where you can edit the commit list.

You’ll see a list of the commits you’re about to squash, along with the word “pick” next to each one.

To squash the commits, replace the word “pick” with “squash” for all commits except for the first one.

The first commit should remain “pick”.

Squash the Commits

After you’ve edited the commit list, save the file and exit the editor.

Git will then squash all of the commits into a single commit.

Edit the Commit Message

After squashing the commits, Git will open another editor where you can edit the commit message for the new, squashed commit.

Enter a new message that summarizes the changes you’ve made in the squashed commits.

Force Push the Branch

Finally, we’ll force push the branch to update the remote repository.

To do this, run the following command in the terminal:

$ git push -f origin <branch name>

Where <branch name> is the name of the branch you’re squashing commits in.

Force pushing should only be done if you’re the only person working on the branch.

If multiple people are working on the same branch, it’s best to avoid force pushing as it can overwrite their changes.


Conclusion

Squashing commits in Git is a powerful technique that can help to simplify the commit history, make it easier to read, and resolve conflicts during a merge.

By following the steps outlined in this post, you can easily squash all commits into a single commit in Git.

Just remember to always check the commit history before squashing commits and use force pushing with caution.

By following these guidelines, you can use the rebase command to efficiently and effectively squash commits in Git.