How to combine multiple commit into single commit in a repo.

How to combine multiple commit into single commit in a repo.

·

4 min read

When working with Git, it's common to have multiple commits in a repository that are related to a specific feature or task. We might do a trial and error phase when creating a new feature. While having multiple commits can be useful for tracking changes and maintaining a history of the project, it can also clutter the commit log like the above it is not useful for us to know about our each and every action and make it difficult to track the progress of the project

Fortunately, Git provides a feature that allows you to combine multiple commits into a single commit. This is known as "squashing" commits, and it can be an effective way to organize your commit history and make it easier to review and manage

we will explore how to combine multiple commits into a single commit in a Git repository using rebasing.

What is Rebase?

Rebasing allows you to integrate changes from one branch into another branch, while maintaining a linear history of the repository. It essentially takes the changes made in one branch and applies them on top of another branch, as if the changes were made directly on the target branch.

In our case, Rebasing can be used to clean up a messy Git history by combining multiple commits into a single, cleaner commit. This can be done by using the git rebase -i command, which allows you to interactively rebase your branch and choose which commits to keep, squash, or discard

Step 1: Identify the Commits to be Squashed

For the example, I have taken the below commits which i need to squash them into a single commit. In visual studio we can view the branch history or otherwise you can use the "git log" command to view the commit history of the repository.

Step 2: Create a New Branch

Before you begin squashing commits, it's a good practice to create a new branch to make changes on. This ensures that you don't accidentally modify the original branch and provides a backup in case something goes wrong during the process.

To create a new branch, use the following command:

git checkout -b <new_branch_name>

Replace <new_branch_name> with a name for your new branch.

Step 3: Start the Interactive Rebase

Once you have identified the commits to be squashed and created a new branch, it's time to start the interactive rebase.

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 that you want to squash. For example, if you want to squash the last three commits, you would use:

git rebase -i HEAD~3

This will open an interactive window that displays a list of the commits to be squashed.

Step 4: Squash the Commits

In the interactive window, you will see a list of commits with the word "pick" next to each commit. To squash commits, you need to replace "pick" with "squash" or "s" for the commits you want to squash.

s 8fd702aa first commit

s 6d592ed0 Second commit

s 16e95dc8 Third commit

Step 5: Edit the Commit Message

After you have squashed the commits, you will be prompted to edit the commit message. This allows you to provide a new commit message that summarizes the changes made in the squashed commits. so the new commit for us is All three commit into single one

Once you have entered the new commit message, save and close the file.

Step 6: Push the Changes

After editing the commit message, you can push the changes to the remote repository. Whenever you have done a squash commit if you are pushing your changes as usual like git push it will throw an error message like below

So after a squash commit always do a force push

So now all three commits are merged into a single commit.

multiple commits into a single commit can help organize the commit history of a Git repository and make it easier to track changes. Rebase is a great tool in re-writing the history of our repo which should be handled with care and knowledge.