Git
How to Squash Commits in GitHub?
When working on a Git-based project, multiple commits often accumulate during feature development or bug fixing. Some of these commits may include unnecessary details, incomplete changes, or fix-ups that clutter the commit history.
Squashing commits allows you to combine multiple commits into one, creating a cleaner and more concise history.
What Is Squashing Commits?
Squashing commits is the process of merging multiple commits into a single commit. Instead of having separate commits like:
commit 1: Added a new feature
commit 2: Fixed a typo in the feature
commit 3: Updated the feature
You can squash them into one:
commit 1: Added the new feature with fixes
Why and When to Squash Commits?
Squashing commits is not always necessary, but it’s particularly useful in the following scenarios:
- Clean Up History: Simplify your commit history by consolidating minor or unnecessary commits.
- Collaborative Projects: Make your changes easier for others to review by reducing clutter.
- Before Merging: Combine all commits related to a single feature or bug fix into one commit for better context.
- Revert-Friendly: A single, concise commit is easier to revert if needed.
How to Squash Commits Using the Command Line
Squashing commits involves using Git’s interactive rebase feature. Follow these steps:
Step 1: Identify the Commits to Squash
Run the following command to view your commit history:
git log --oneline
This will display a list of commits like:
abc123 Updated README file
def456 Added new feature
ghi789 Fixed typo in the feature
Determine how many commits back you want to squash. For example, to squash the last three commits into one, proceed to the next step.
Step 2: Start an Interactive Rebase
Run the interactive rebase command with the number of commits to include:
git rebase -i HEAD~3
Replace 3
with the number of commits you want to squash.
Step 3: Mark Commits for Squashing
The interactive rebase window will open in your default text editor (e.g., Vim, Nano). You’ll see something like this:
pick abc123 Updated README file
pick def456 Added new feature
pick ghi789 Fixed typo in the feature
- The first commit (
pick
) will remain as the base commit. - For subsequent commits, change
pick
tosquash
(ors
):
pick abc123 Updated README file
squash def456 Added new feature
squash ghi789 Fixed typo in the feature
Save and close the editor.
Step 4: Combine Commit Messages
After marking commits for squashing, Git will prompt you to combine their commit messages. You’ll see something like this:
# This is a combination of 3 commits.
# The first commit’s message is:
Updated README file
# The following commit messages will also be included:
Added new feature
Fixed typo in the feature
Edit the message to create a single, meaningful commit message:
Updated README file and added a new feature with typo fixes
Save and close the editor.
Step 5: Finalize the Rebase
Git will replay the commits and squash them. If there are no conflicts, you’re done. Use the following command to confirm the new history:
git log --oneline
The squashed commits will now appear as a single commit.
Step 6: Push Changes (If Necessary)
If the changes have already been pushed to a remote repository, you’ll need to force-push the branch after squashing:
git push origin branch-name --force
How to Squash Commits in GitHub Pull Requests
If you’re working in a pull request (PR) on GitHub, commits can also be squashed during the merge process.
Step 1: Open the Pull Request
- Go to your repository on GitHub.
- Open the pull request you want to merge.
Step 2: Select “Squash and Merge”
- Click the green “Merge” button.
- Select “Squash and Merge” from the dropdown options.
Step 3: Edit the Commit Message
GitHub will display a consolidated commit message, combining the messages from all the commits in the PR. You can edit this to create a single, clear message.
Step 4: Confirm the Merge
Click “Confirm Squash and Merge” to merge the pull request. All commits in the PR will be squashed into one in the target branch.
Best Practices for Squashing Commits
- Squash Before Merging: Keep the main branch history clean and concise.
- Use Descriptive Messages: Write a single, meaningful commit message summarizing all changes.
- Coordinate with Teams: If others are working on the same branch, communicate before force-pushing.
- Preserve Important Details: Avoid squashing commits that represent distinct, meaningful steps in development.
Conclusion
Squashing commits is a powerful way to simplify Git history, reduce clutter, and make repositories easier to maintain. Whether you’re using the command line or GitHub’s squash-and-merge feature, the process is straightforward and highly effective.
By mastering this technique, you’ll contribute to cleaner repositories and smoother collaboration.