Open In App

Use of Git Squash Commits

Last Updated : 22 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

When working with Git, especially in feature branches or collaborative projects, you may end up with multiple commits that you’d prefer to combine into a single commit before merging. Squashing commits in Git helps maintain a clean and organized commit history. In this article, we’ll learn about squashing commits in Git.

What Is Commit Squashing?

Commit squashing is the process of combining multiple commits into a single, more meaningful commit. This is often done using Git’s interactive rebase feature. When squashing commits, you can take several smaller commits and turn them into a single commit that better represents the purpose or intent of the code changes.

Why Squash Commits?

  • Cleaner Commit History: A clean commit history makes it easier for other developers to understand the evolution of the project.
  • Logical Grouping of Changes: Squashing allows you to group related changes together in one commit.
  • Simplifies Code Reviews: Fewer commits make code reviews easier and more efficient.
  • Prepares Code for Merging: Before merging a feature branch, it’s common to squash commits to avoid cluttering the main branch with minor or redundant commits.

Uses of Squashing Commits

  • Feature Development: When working on a feature branch, you might have many small commits. Before merging the branch into the main, you can squash them into one concise commit.
  • Bug Fixes: If you have multiple commits related to a single bug fix, squashing them keeps your history clean.
  • Collaborative Branches: When working with others, you can squash commits before merging to ensure a clean and easy-to-follow commit history.

How to Squash Commits Using Interactive Rebase?

Interactive rebase is the most common method for squashing commits. It allows you to rewrite your commit history and choose which commits to squash.

Steps to Squash Commits Using Interactive Rebase

1. Determine How Many Commits to Squash:

First, figure out how many commits back you want to start squashing. For example, if you want to squash the last 4 commits, you’d use:

git rebase -i HEAD~4

2. Pick and Squash Commits in Interactive Mode:

The above command opens an editor with a list of your commits:

pick 1234567 Commit message 1
pick 2345678 Commit message 2
pick 3456789 Commit message 3
pick 4567890 Commit message 4

Change the word pick to squash (or s) for the commits you want to squash into the previous commit:

pick 1234567 Commit message 1
squash 2345678 Commit message 2
squash 3456789 Commit message 3
squash 4567890 Commit message 4

3. Edit the Commit Message:

After choosing which commits to squash, Git will prompt you to combine and edit the commit message. You can either keep the default combined messages or write a new, more descriptive message.

4. Save and Exit:

Once you’re happy with the commit message, save and exit the editor. Git will apply the rebase and squash the commits.

5. Force-Push the Squashed Commits:

If you’ve squashed commits on a branch that’s already been pushed to a remote repository, you’ll need to force-push the changes:

git push --force

How to Squash the Last Few Commits?

If you want to squash only the most recent commits (e.g., the last 2 commits), you can use:

git rebase -i HEAD~2

Follow the same process as described above, changing the pick keyword to squash for the commit you want to squash.

Squashing Commits During a Merge

Git also allows you to squash commits while merging a branch. This is especially useful when you want to keep the commit history clean during the merge.

git merge --squash <branch>

This command squashes all commits from the specified branch into a single commit before merging it into the current branch. After running the command, you’ll need to commit the squashed changes:

git commit -m "Your commit message"

Best Practices for Squashing Commits

  • Squash Before Pushing: Squash your commits before pushing them to a remote repository to avoid rewriting shared history.
  • Use Meaningful Commit Messages: When squashing, create a descriptive commit message that summarizes the changes effectively.
  • Communicate with Your Team: If working in a team, communicate your intention to squash commits, especially if you’ll be force-pushing.

Troubleshooting Common Issues

  • Rebase Conflicts: If you encounter conflicts during the rebase, Git will prompt you to resolve them. After resolving conflicts, continue the rebase process:
git rebase --continue
  • Accidentally Squashed the Wrong Commits: If you made a mistake during the rebase, you can abort it and return to the previous state:
git rebase --abort
  • Force-Pushing After Squashing: When you squash commits and rewrite history, you must use git push --force to update the remote branch.

Next Article
Article Tags :

Similar Reads