Introduction
Squashing commits in Git is a way of combining multiple commit changes into a single commit. This is particularly useful for cleaning up your commit history, merging small fixes, or consolidating changes made over several commits into a more coherent package. This tutorial will guide you through squashing commits using Lazygit.
Lazygit
Here’s how to squash commits in Lazygit:
-
Select the Commit to Squash:
- Open Lazygit in your repository.
- Navigate to the Commits panel by pressing
1
. - Hover over the later commit that you want to squash into a prior commit. This is typically a more recent commit that you want to combine with an older one.
-
Squash the Commit:
- With the commit selected, press
s
to squash it down. This action combines the selected commit with the one directly below it. - A dialog box may appear asking you to edit the commit message for the new, squashed commit. This is where you can combine the messages from the squashed commits or write a new one.
- With the commit selected, press
-
Finalize the Squash:
- Complete the squash operation. Lazygit will automatically handle the rebase process to rewrite the commit history.
- Once completed, the changes from the squashed commit will be merged into the previous commit, and the commit history will be updated.
Basic Terminal
To squash commits using the terminal:
-
Start an interactive rebase:
Replace
<number>
with the count of commits you want to include in the rebase. -
In the interactive rebase list, mark the commit you want to squash with
squash
ors
next to it. -
Save and close the editor. Git will then prompt you to merge the commit messages.
-
After adjusting the commit message, continue the rebase:
Conclusion
Squashing commits in Lazygit simplifies the process of combining multiple commits into one, making your project history cleaner and more understandable. The terminal method is a more manual approach but provides greater control over the squashing process. Remember, squashing rewrites your project’s history, so it should be done with caution, especially when working with shared branches or after pushing to remote repositories.