How to use the command "git squash" (with examples)
The “git squash” command is a part of the git-extras package that allows you to combine multiple commits into a single commit. This can be useful for cleaning up your commit history or creating more cohesive and organized commits.
Use case 1: Merge all commits from a specific branch into the current branch as a single commit
Code:
git squash source_branch
Motivation:
Merging all the commits from a specific branch into the current branch as a single commit can help to simplify the commit history and make it easier to understand the changes made in a particular branch. It also allows for more efficient collaboration with other developers, as it reduces the noise caused by unnecessary commit messages.
Explanation:
git squash
: The command itself that triggers the squashing of commits.source_branch
: The branch from which you want to merge the commits. This can be any valid branch name.
Example OUTPUT:
Squashing commits from source_branch...
1 file changed, 4 insertions(+), 3 deletions(-)
Use case 2: Squash all commits starting with a specific commit on the current branch
Code:
git squash commit
Motivation:
Squashing all commits starting from a specific commit on the current branch can help to create a more concise commit history. It allows you to group related changes together and provide a clearer explanation of the changes made.
Explanation:
git squash
: The command itself that triggers the squashing of commits.commit
: The specific commit from which you want to start squashing the commits. This can be the commit hash, a branch name, or a tag.
Example OUTPUT:
Squashing commits starting from commit...
1 file changed, 10 insertions(+), 8 deletions(-)
Use case 3: Squash the n
latest commits and commit with a message
Code:
git squash HEAD~n "message"
Motivation:
Squashing the n
latest commits and providing a commit message allows you to summarize a series of commits into a single, more meaningful commit. This can be useful when you have made multiple small changes that are related and can be better represented as a single commit.
Explanation:
git squash
: The command itself that triggers the squashing of commits.HEAD~n
: Specifies the numbern
of latest commits to squash.HEAD~n
refers to the commit that isn
steps behind the current commit."message"
: The commit message that will be used for the new commit.
Example OUTPUT:
Squashing 4 latest commits...
1 file changed, 20 insertions(+), 12 deletions(-)
Use case 4: Squash the n
latest commits and commit concatenating all individual messages
Code:
git squash --squash-msg HEAD~n
Motivation:
Squashing the n
latest commits and concatenating all individual commit messages can be useful when you want to provide a more detailed summary of the changes made in each commit. This allows you to preserve the individual commit messages while still consolidating the changes into a single commit.
Explanation:
git squash
: The command itself that triggers the squashing of commits.--squash-msg
: Specifies that the individual commit messages should be concatenated into a single commit message.HEAD~n
: Specifies the numbern
of latest commits to squash.HEAD~n
refers to the commit that isn
steps behind the current commit.
Example OUTPUT:
Squashing 3 latest commits and concatenating messages...
1 file changed, 8 insertions(+), 4 deletions(-)
Conclusion:
The “git squash” command is a powerful tool for managing your commit history and creating more cohesive and organized commits. By squashing multiple commits into a single commit, you can simplify your commit history, provide clearer explanations of changes, and improve collaboration with other developers.