Horje
Git - Squash Commits

In Git, squashing refers to combining two or more commits into a single commit. This is typically done to improve the organization and clarity of the commit history, especially when working on a collaborative project with other developers. In this article, we will review the feature git squashing, which is a great feature for arranging multiple commits into a single one. Git squashing is done by merging the previous **N** number of commits into a single commit. Use the git rebase -i HEAD~<no of commits to consider>. For example:

Command: git rebase -i HEAD~3

Command: git rebase -i HEAD~3

This command will open the git-rebase-todo file in your configured text editor.

Note: Please note that you cannot include the first commit with this command.

The next step is to substitute all “pick” occurrences with “squash“, except for the initial one. You will merge your changes into the first commit without losing any data.

Replace pick with squash

Replace pick with squash

Upon completion, save and close the file. Git will subsequently launch a new editor, which displays the newly generated commit message.

Newly generated commit message

Newly generated commit message

You have the option to remove all of them and insert your own personalized message.

New commit message

New commit message

Upon completion of the process, a message indicating success should be displayed in the terminal.

Successfully rebased and updated

Successfully rebased and updated

You can use the git log command to check whether your messages are successfully squashed or not. 

To undo squashing use this command:

git reset --hard HEAD@<no of commits>



Reffered: https://www.geeksforgeeks.org


Git

Related
Git - Create a Branch From Another Branch Git - Create a Branch From Another Branch
First Open Source Contribution to GitHub - A Step By Step Guide First Open Source Contribution to GitHub - A Step By Step Guide
Git Tutorial Git Tutorial
What is GitHub README File and Markdown? What is GitHub README File and Markdown?
Deployment of React Application using GitHub Pages Deployment of React Application using GitHub Pages

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
14