Home/Blog/Mastering Git: How to Squash Commits and Streamline Your History
Software Engineering

Mastering Git: How to Squash Commits and Streamline Your History

Learn how to squash multiple Git commits into a single commit using interactive rebase to create a cleaner, more maintainable commit history.

By Inventive HQ Team
Mastering Git: How to Squash Commits and Streamline Your History

When you are working with version control, you may have multiple commits that you would like to combine into a single commit. Or perhaps you want to change their order, clean up your commit history, or prepare changes for a pull request. Git's interactive rebase feature makes this easy.

Why Squash Commits?

Squashing commits is useful when you want to:

  • Combine multiple small commits into one meaningful commit
  • Clean up your commit history before merging to main/master
  • Group related changes together
  • Remove "work in progress" or "fix typo" commits
  • Create a cleaner, more professional commit history

Finding the Commits to Squash

First you need to determine how far back you want to go. Open a command prompt or terminal window, navigate to your Git Repository.

Next, type:

git log -10

The above command will show the last 10 commits. If that does not go far enough back, increase the number 10.

Starting Interactive Rebase

Once you have identified the commits you wish to squash, run the git rebase command:

git rebase -i HEAD~10

The above command will open a git rebase window showing you the last 10 commits. Adjust this number as needed.

Understanding the Rebase Interface

Each line shown will have the word "pick" at the beginning. Here is what the rebase window looks like:

pick abc1234 First commit message
pick def5678 Second commit message
pick ghi9012 Third commit message

# Rebase commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
# d, drop = remove commit

Squashing Your Commits

Any commits you wish to squash, replace the word "pick" with the letter "s" (for squash) or "f" (for fixup).

Important guidelines:

  • Leave the first commit in the list set to "pick" - this is what other commits will be squashed into
  • Replace "pick" with "s" for commits you want to squash into the previous commit
  • Be sure to leave at least one commit as "pick" so there's something to squash into

For example, to squash the second and third commits into the first:

pick abc1234 First commit message
s def5678 Second commit message
s ghi9012 Third commit message

Editing the Combined Commit Message

After you save and close the rebase file, Git will prompt you to edit the combined commit message. You can:

  • Keep all the original commit messages
  • Write a new single message that describes all the changes
  • Delete unwanted messages and keep only the relevant ones

Completing the Rebase

Once you've edited the commit message and saved the file, Git will complete the rebase and your commits will be squashed together.

Important Notes

  1. Don't rebase published commits: Only squash commits that haven't been pushed to a shared repository, or be prepared to force push
  2. Force push carefully: If you've already pushed the commits, you'll need to force push: git push --force
  3. Backup your work: Consider creating a backup branch before rebasing: git branch backup-branch

Common Issues and Solutions

Issue: "Cannot 'squash' without a previous commit"

  • Solution: Make sure the first commit in your rebase list is set to "pick"

Issue: Merge conflicts during rebase

  • Solution: Resolve conflicts, then run git rebase --continue

Issue: Want to abort the rebase

  • Solution: Run git rebase --abort to return to the state before rebasing

Best Practices

  1. Squash commits before creating a pull request
  2. Keep meaningful commit messages in the final squashed commit
  3. Don't squash commits that have already been merged to main
  4. Use descriptive commit messages that explain the "why" not just the "what"
  5. Consider your team's commit history preferences before squashing

Mastering Git's rebase and squash features will help you maintain a clean, professional commit history that makes code review and debugging much easier.

Need Expert IT & Security Guidance?

Our team is ready to help protect and optimize your business technology infrastructure.