Git

How Can I Merge Multiple Commits Onto Another Branch as a Single Squashed Commit?

Developers can perform multiple operations using Git, such as creating and deleting files or folders, creating, and deleting branches, merging branches, and commits. When the developer’s Git commit history gets long, they need to clean up their feature branches before merging to the master. For this purpose, the Git squash commit features can be used. More specifically, squashing Git commits refers to the process of combining more than one commit from existing log history into a single one.

This blog explains the procedure to combine several commits onto another Git local branch as a single squashed commit.

How Can I Merge Multiple Commits Onto Another Git Local Branch as a Single Squashed Commit?

To combine several commits onto another branch as a single squashed commit, first, navigate to the Git local repository and create a new file. Track it and update the repository by committing. Then, update the file and add changes to the repository. Next, create a new local branch and immediately switch to it. Execute the “$ git merge –squash <branch>” command to merge the commits. Lastly, run the “git commit” command to save the changes.

Now, implement the above-discussed instructions!

Step 1: Move to Git Local Repository

First, navigate to the desired local repository using the “cd” command:

$ cd "C:\Users\nazma\Git\Test_2"

Step 2: Create File

Next, create a new file by using the “touch” command along with the file name:

$ touch file1.txt

Step 3: Track File

Execute the “git add” command to track the created file:

$ git add file1.txt

Step 4: Update Repository

Next, save the added changes to the local repository through the “git commit” command with the “-m” option to add the desired commit message:

$ git commit -m "1 file added"

Step 5: Update File

Open the created file using the “start” command with a default text editor and update it:

$ start file1.txt

Step 6: Track Added Changes

Track all added changes to the repository using the “git add .” command:

$ git add .

Step 7: Commit Changes

Execute the “git commit” command with the “-m” option to commit all changes and save them:

$ git commit -m "1 file updated"

Step 8: Git Log History

To view the current Git repository log history, execute the “git log .” command:

$ git log .

Step 9: Create and Switch Local Branch

Run the “git checkout” command with the “-b” option to create and switch to the branch immediately:

$ git checkout -b gemma

In the above command, “gemma” is specified as the branch name:

Step 10: Merge Multiple Commits

Merge the multiple commits onto another branch as a single squashed commit by executing the “git merge” command along with the “–squash” option:

$ git merge --squash main

As you can see in the below-provided output, the most recent two commits are merged:

Step 11: Update Repository

Now, execute the “git commit” command to update the repository:

$ git commit

The below output indicates that the commits are squashed successfully onto another branch:

Step 12: Verify Git Log History

Lastly, execute the following command for verification:

$ git log .

It can be observed that the merging operation of multiple commits is performed successfully:

That’s it! We have provided the method to combine several commits onto another Git local branch as a single squashed commit.

Conclusion

To combine several commits onto another branch as a single squashed commit, move to the Git local repository and create a new file. Then, track it and update the repository by committing. After that, update the file and add changes to the repository. Next, create a new local branch and immediately switch to it. Execute the “$ git merge –squash <branch>” command to merge the commits. Run the “git commit” to save changes. This blog demonstrated the method to combine several commits onto another Git local branch as a single squashed commit.

About the author

Maria Naz

I hold a master's degree in computer science. I am passionate about my work, exploring new technologies, learning programming languages, and I love to share my knowledge with the world.