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:
Step 2: Create File
Next, create a new file by using the “touch” command along with the file name:
Step 3: Track File
Execute the “git add” command to track the created file:
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:
Step 5: Update File
Open the created file using the “start” command with a default text editor and update it:
Step 6: Track Added Changes
Track all added changes to the repository using the “git add .” command:
Step 7: Commit Changes
Execute the “git commit” command with the “-m” option to commit all changes and save them:
Step 8: Git Log History
To view the current Git repository log history, execute the “git log .” command:
Step 9: Create and Switch Local Branch
Run the “git checkout” command with the “-b” option to create and switch to the branch immediately:
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:
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:
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:
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.