How to Use the Command 'git squash' (with Examples)
The git squash
command is an essential tool in the developer’s toolkit, providing the functionality to combine multiple commits from a Git repository into a single commit. This operation is useful for streamlining commit history, making it more readable and concise. It helps in maintaining a clean and organized repository by reducing the clutter of excessive commits. The git squash
command is a part of the git-extras
package, which offers a suite of additional Git functionalities, enhancing the capabilities of the default Git installation.
Use case 1: Merge all commits from a specific branch into the current branch as a single commit
Code:
git squash source_branch
Motivation:
This use case is immensely useful when you want to integrate changes from a feature branch back into a main branch or another long-lived branch, such as main
or develop
, in a cleaner way. Rather than merging and preserving the history of numerous small and perhaps trivial commits made during development, squashing them into a single commit simplifies the history. This is beneficial for review purposes, ensuring that the main branch remains tidy without the noise of intermediary steps.
Explanation:
git squash
: The command initiates the squashing operation.source_branch
: This argument specifies the branch from which all commits should be taken and squashed into a single commit on the current branch. It signifies the branch that holds the changes you wish to combine.
Example Output:
Upon execution, you may see output indicating that the commits have been successfully combined and any potential conflicts have been resolved. A message like “Squashed all commits from source_branch into one” might be displayed, confirming the successful squashing.
Use case 2: Squash all commits starting with a specific commit on the current branch
Code:
git squash commit
Motivation:
This usage scenario is pertinent when you want to clean up a series of commits starting from a particular commit in the branch’s history. For instance, if you’ve made a series of experimental changes that eventually led to a stable solution, you might want to squash these into a single commit. This simplifies the commit history and makes understanding the project evolution more straightforward for future collaborators or reviewers.
Explanation:
git squash
: This initiates the squash process, combining the specified commits.commit
: This argument refers to the hash or identifier of the first commit in the current branch from which subsequent commits will be squashed. It acts as the starting point for the squash.
Example Output:
The output will confirm the commit history condensation from the specified starting point. Feedback such as “Starting from commit
Use case 3: Squash the n
latest commits and commit with a message
Code:
git squash HEAD~n "message"
Motivation:
This use case is highly useful when you aim to squash the most recent changes and provide a concise commit message summarizing the work done. Developers often make numerous small commits during the process of fixing a bug or implementing a feature. Before merging these changes into the main branch, it’s prudent to squash them and attach a unified, descriptive commit message to succinctly explain the impact or purpose of the changes.
Explanation:
git squash
: This serves as the command to execute the squash.HEAD~n
: Here,n
represents the number of most recent commits on the current branch to be squashed.HEAD~n
specifies the starting point, wheren
is the number of previous commits relative to the current commit."message"
: This argument is the commit message that will be associated with the newly squashed single commit. It provides a clear summary of the combined changes.
Example Output:
Upon completion, the new single commit will bear the provided message, and an output statement such as “Squashed last n commits with message: ‘message’” will confirm success.
Use case 4: Squash the n
latest commits and commit concatenating all individual messages
Code:
git squash --squash-msg HEAD~n
Motivation:
In some contexts, it might be beneficial to preserve the history of messages from the individual commits being squashed. This can be crucial for documentation purposes or when individual commit messages contain significant details that collectively narrate a complete story of the changes made. Concatenating these messages captures the essence of each step while simplifying the commit structure.
Explanation:
git squash
: This triggers the squash command.--squash-msg
: This flag tells Git to concatenate the messages from all the squashed commits and use the combined string as the commit message.HEAD~n
: This points to the n-th commit before the current commit, guiding the squash operation to consider these latestn
commits.
Example Output:
The concatenated message from the n squashed commits will be presented, typically prefixed with a confirmation message like “Squashed last n commits with concatenated messages.”
Conclusion:
The git squash
command is a powerful utility for anyone seeking a cleaner, more comprehensible Git history. By combining multiple commits into one, this command helps maintain a well-organized repository, facilitating easier collaboration and comprehension among team members. Whether you’re integrating a feature branch, cleaning up experimental work, or simply wanting to present a cohesive commit history, git squash
provides the versatility and simplicity needed to manage complex versioning tasks effectively.