Graphite Reviewer is now Diamond

Git merge squash

Greg Foster
Greg Foster
Graphite software engineer
Try Graphite


Note

This guide explains this concept in vanilla Git. For Graphite documentation, see our CLI docs.


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."

Utilizing git merge --squash in your Git workflow can offer several advantages, particularly in maintaining a streamlined and manageable project history.

Squash merging consolidates all changes from a feature branch into a single commit on the target branch. This approach results in a linear and uncluttered commit history, making it easier to navigate and understand the evolution of the codebase.

By combining multiple commits into one, reverting a feature becomes straightforward. If a newly introduced feature causes issues, you can easily roll back the entire set of changes with a single git revert command, reducing the complexity of pinpointing and reversing individual commits.

Presenting all related changes in a single commit can streamline the code review process. Reviewers can assess the complete set of modifications in one go, without sifting through numerous intermediate commits, which may include minor fixes or adjustments.

A linear commit history is beneficial for continuous integration (CI) systems. It simplifies processes like automated testing, deployment, and debugging by providing a clear and sequential record of changes. This clarity can lead to more efficient CI pipelines and easier identification of issues when they arise.

Incorporating git merge --squash into your development practices can lead to a more organized and efficient workflow, particularly in collaborative environments where clarity and simplicity are paramount.

Join 45,000+ developers at top companies
Stop wrestling with Git commands
The Graphite CLI takes all the pain out of Git, allowing you to ship faster and stop googling Git commands.
main
diff1
diff2

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

  • Conflicts: Just like regular merges, squash merges can also lead to conflicts, which you will have to resolve manually.
  • Not suitable for all scenarios: 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.
Join 45,000+ developers at top companies
The best engineers use Graphite to simplify Git
Engineers at Vercel, Snowflake & The Browser Company are shipping faster and staying unblocked with Graphite.
main
diff1
diff2
QuestionAnswer
Can I squash merge into any branch, or does it have to be the main branch?You can squash merge into any branch, not just the main or master branch. The choice of the target branch depends on your workflow.
How do I resolve conflicts during a squash merge?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.
Can I undo a squash merge?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.
Is git merge --squash recommended for long-running branches?Generally, no. Squash merges are better suited for feature branches that are relatively short-lived and will be merged and deleted soon after.
Can I pick which commits to squash?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.
What happens to the feature branch after a squash merge?The feature branch remains unchanged after a squash merge. You can choose to delete it manually if it's no longer needed.
Can I use squash merge in GUI Git clients?Yes, most GUI Git clients offer an option to perform squash merges. The exact steps can differ between clients.
Is squash merging a good practice for open-source projects?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.

Built for the world's fastest engineering teams, now available for everyone