Git merge squash

Greg Foster
Greg Foster
Graphite software engineer

git merge --squash is a Git operation that allows you to take all the changes from one branch and squash them into a single commit on top of the branch you're currently on. This is different from a regular merge, where all commits from the feature branch are preserved. By squashing them into one, you're essentially saying, "Take all these changes as if they happened in a single moment."


One of the main reasons to use squash merging is to maintain a clean and linear project history.

Because all changes from the feature branch are condensed into a single commit, reverting changes is straightforward.

When changes are packed into a single commit, it makes the code review process much easier.


Here are the steps to perform a squash merge:

  1. Checkout to the Base Branch: Make sure you are on the branch you want to merge into.

    git checkout master

  2. Perform the Squash Merge: Use the git merge --squash command followed by the feature branch name.

    git merge --squash feature-branch

  3. Commit the Changes: Finally, commit the changes.

    git commit -m "Squashed commit from feature-branch"


While Git is an incredibly useful tool, it has many shortcomings, particularly with rebasing, and managing stacked pull requests.

The Graphite CLI simplifies git, handles rebasing automatically, and allows you to create, submit, and stack pull requests right from the command line.

Under the hood, the CLI runs Git to create branches, commits, and metadata, which means you can still use Git in your scripts, tooling, or whenever you feel like it. Read more about installing the Graphite CLI in our docs.

screenshot of the Graphite CLI


Just like regular merges, squash merges can also lead to conflicts, which you will have to resolve manually.

Squash merges are not ideal for branches with extensive history or for branches intended to be temporary.


  • When you want to preserve the context provided by multiple commits.

  • When changes are too extensive and need to be reviewed individually.


  • Regular Merge: Keeps the entire commit history of the feature branch.

  • Rebase: Moves the feature branch on top of the master, creating a linear history without squashing.


FAQ

A: The git merge --squash command takes all the commits from a feature branch and squashes them into a single commit on your current branch. This allows you to have a single commit that includes all changes, rather than a commit for each change made in the feature branch.


A: While both can be used to create a linear history, git rebase moves the entire feature branch to the tip of the master branch, preserving individual commits. git merge --squash, however, squashes all changes into one single commit, making the history cleaner but losing the granularity of individual commits.


A: You can squash merge into any branch, not just the main or master branch. The choice of the target branch depends on your workflow.


A: Conflict resolution during a squash merge is similar to a regular merge. You'll need to manually edit the conflicting files, mark them as resolved, and then commit the changes.


A: Yes, you can undo a squash merge by running git reset --hard <previous_commit_hash>. However, be cautious with this command, especially on shared branches, as it rewrites history.


A: Generally, no. Squash merges are better suited for feature branches that are relatively short-lived and will be merged and deleted soon after.


A: The git merge --squash command squashes all commits from the feature branch. If you want to pick specific commits, you should look into using git rebase -i for an interactive rebase.


A: The feature branch remains unchanged after a squash merge. You can choose to delete it manually if it's no longer needed.


A: Yes, most GUI Git clients offer an option to perform squash merges. The exact steps can differ between clients.


A: The use of squash merging in open-source projects can be contentious. Some maintainers prefer a clean history, while others prefer to preserve the entire history for context. It's best to check the project's contribution guidelines.

Give your PR workflow
an upgrade today

Stack easier | Ship smaller | Review quicker

Or install our CLI.
Product Screenshot 1
Product Screenshot 2