Squashing commits in Git is a powerful technique that allows developers to clean up their commit history before merging changes into the main branch. However, there are times when you might squash too aggressively or realize that you need the original commits separated again. If you find yourself in this situation, don’t worry! Git provides several methods to undo a squash and restore your previous commits. In this comprehensive guide, we will explore various strategies to achieve this, complete with examples, visuals, and best practices.
What Is Git Squashing?
Squashing commits is the process of combining multiple commits into a single commit. This is often done to create a cleaner commit history before merging changes into the main branch. Squashing is typically performed using interactive rebase.
Example of Squashing Commits
Suppose you have the following commits in your branch:
abc1234 Commit 1 - Add user model
def5678 Commit 2 - Add email validation
ghi9012 Commit 3 - Fix typo in validation
To squash these commits, you would run:
git rebase -i HEAD~3
This command opens an editor where you can choose to pick
the first commit and squash
the subsequent commits. After squashing, your commit history might look like this:
xyz3456 Commit SQUASHED - Add user model with validation
Now, what if you realize you need those original three commits back? Let’s explore how to undo a Git squash.
Read more »Labels: How to Undo a Git Squash and Restore Previous Commits (Even After Push)