Git Squash Tutorial
Overview
Git squash allows you to combine multiple commits into a single commit. This is particularly useful for cleaning up your commit history before merging a feature branch into the main branch. This document will cover the basics of using git squash with examples.
What is Git Squash?
Git squash is the process of combining multiple commits into one. This helps to simplify the commit history and make it more readable.
Interactive Rebase for Squashing Commits
The most common way to squash commits is by using an interactive rebase. This allows you to choose which commits to squash and how to combine them.
Example: Squashing the Last Two Commits
-
Start an interactive rebase for the last two commits:
-
In the interactive rebase editor, you will see something like this:
-
Change the second
pick
tosquash
(ors
): -
Save and close the editor. Another editor will open to combine the commit messages. Edit as necessary, then save and close.
-
Your commits are now squashed into one.
Example: Squashing Multiple Commits
-
Start an interactive rebase for the last four commits:
-
In the interactive rebase editor, you will see something like this:
-
Change all but the first
pick
tosquash
(ors
): -
Save and close the editor. Another editor will open to combine the commit messages. Edit as necessary, then save and close.
-
Your commits are now squashed into one.
Using commit --squash
The --squash
option can be used with git commit
to combine changes into a previous commit without immediately amending the commit.
Example
-
Make some changes and stage them:
-
Use
commit --squash
to prepare to squash these changes into a previous commit: -
Complete the squash by performing a rebase:
-
Follow the interactive rebase steps as described earlier to finalize the squash.
Squashing Commits During a Merge
When merging a feature branch into the main branch, you can squash all commits into a single commit.
Example
-
Check out the main branch:
-
Merge the feature branch with the
--squash
option: -
Commit the squashed changes:
This will combine all the commits from feature-branch
into a single commit on main
.
Useful Tips
- Backup Important Changes: Before performing a rebase or squash, ensure you have backups of important work or work on a separate branch.
- Interactive Rebase: Use
git rebase -i
for greater control over how commits are squashed and merged. - Use Descriptive Commit Messages: When combining commit messages during a squash, make sure to create a comprehensive and descriptive message that accurately reflects the changes.
Summary
Git squash is a powerful tool for cleaning up commit history and making it more readable. By understanding how to use interactive rebase, commit --squash
, and squashing during merges, you can maintain a cleaner and more manageable repository.