Squash commits when merging a Git branch with Bitbucket

Imagine you’re working on a feature. You create a pull request with your changes and get some feedback. You update your pull request by adding another commit that addresses the feedback. Maybe you notice a typo. So you create another commit that fixes the typo. Very soon, your feature branch has a lot going on with all these commits:

So you get your PR approvals and you merge. But looking through your repository’s history, you notice that it looks busy. A lot of these commits don’t actually add any value to your repo’s history. They clutter up the blame, make bisects take longer and generally make the history hard to navigate.

Squash your commits in Bitbucket Cloud

You could always squash commits via the command line using “git merge –squash”, but this is just another time consuming step, and it would be a lot easier to have it done for you from Bitbucket. That’s why we added the ability for Git users to squash commits in feature branches when merging pull requestsCombining these commits will provide a clean, easy-to-follow history for your repo. Squash merge is available as a default merge strategy in Bitbucket: 

What merge strategy is right for you?

When to do a merge commit

Select Merge commit when you want to maintain an exact history of changes. Merge commits are also useful if, as part of your workflow, pull requests are large in scope and you review commits individually. Because this strategy keeps all commits during the merge, you’ll still see all commits from the source branch on the Commits page.

When to do a squash merge

Select Squash to make your commits list less cluttered, which results in less time to search for commits that introduce a bug (with a git bisect) and provides an easy-to-follow commit history. Because this strategy combines all commits when you merge, you’ll only see one commit on the destination branch on the Commits page.

When to do a fast-forward merge

Select Fast forward if the destination branch has no new commits since you created the source branch. Fast-forward merges move the source branch tip up to the destination branch tip, combining commit histories. Because this strategy moves the source branch’s commits to the destination branch, you’ll still see all commits on the Commits page.

Try Merge commit, Squash merge, or fast-forward merge

Next time you want to merge a pull request, try out the merge commit, squash merge, or fast-forward merge.

If you’re new to Bitbucket, sign up for an account, import some code, add your team mates and have them review your code via a pull request. When you are ready to merge their feedback, you will find the merge strategies.

Looking for more in depth information on this new feature? More information on merge strategies can be found here.

Have more specific questions about this post? Reach out to us on Twitter to get the information you need. Looking for squash merges in Bitbucket Server? Merge strategies are available in Bitbucket Server 4.9.