Bitbucket Cloud: a pull request merged by itself
Nobody pressed Merge, yet the pull request says MERGED, perhaps with its merge checks still unmet. Bitbucket Cloud does this on purpose: once the destination branch has every commit the pull request would bring, or the pull request has no changes left, there is nothing more for it to merge, and Bitbucket marks it as merged (Atlassian's knowledge base, Pull request merged automatically).
The three ways it happens
- Another pull request merged its commits. branch1 has commit1, with pull request PR1 into main. branch2 was made from branch1 and adds commit2, with PR2 into main, so PR2 carries commit1 and commit2. When PR2 merges, commit1 is in main, PR1 has nothing left to merge, and Bitbucket marks PR1 as merged too, even if its merge checks are incomplete.
- Someone merged it outside the pull request. The source branch was merged in a clone (
git checkout main,git merge feature) and the merge commit pushed to main. The pull request finds its changes in main and is marked as merged, bypassing its merge checks, even on Premium with Prevent a merge with unresolved merge checks on (Atlassian's knowledge base, Why a pull request is merged despite failing the merge checks). - The pull request lost its changes. A rebase or a force push took the source branch back to before its own commits. The diff becomes empty, and with no changes to merge, the pull request is marked as merged.
In the IDE: Pull Request Inbox for Bitbucket, a plugin for JetBrains IDEs, lists the pull requests that wait for your review, with their diffs and which merge checks block them, so you review each one before it merges.
Tell which it was
Take the source branches of the two pull requests and find where they meet:
git fetch origin
git merge-base origin/branch1 origin/branch2
git log --oneline $(git merge-base origin/branch1 origin/branch2)..origin/branch1
git log --oneline $(git merge-base origin/branch1 origin/branch2)..origin/branch2
The branch of the pull request that merged by itself has no commits after the merge base; the other one has them all. If the destination has a merge commit of the source branch that no pull request made, it was merged outside: a local git merge feature writes Git's own message, Merge branch 'feature'. If there is no second pull request, look at the source branch's history for a force push that dropped the commits.
Keep it from happening
- Point a stacked pull request at the branch below it. PR2 into branch1 instead of main carries only commit2, so merging it doesn't take PR1's commits along. When PR1 has merged, and before branch1 is deleted, change PR2's destination to main (changing the destination branch).
- Limit who can push to the destination branch. Merge checks guard the merge made in Bitbucket, not a push. In Repository settings > Branch restrictions, add a restriction for the branch and under Write access choose Only specific people or groups have write access, as Atlassian suggests.
- Merge in order. Merge PR1 before PR2, so PR1 passes its own checks and review.
- Don't rewrite a branch under an open pull request back past its own commits. If its work is gone on purpose, decline the pull request instead (declining).
Bitbucket pull requests in IntelliJ IDEA
Pull Request Inbox for Bitbucket, a plugin by Lesstep for JetBrains IDEs 2024.1 or later, lists the pull requests that wait for your review in an IDE tool window, with their diffs and discussions, and shows next to each which merge checks pass and which block the merge. The review inbox, diffs, discussions and merge checks are free; approving, commenting and merging are Pro, with a 30-day trial. It also works with Bitbucket Data Center (beta).
Get it on JetBrains Marketplace
Bitbucket and Atlassian are trademarks of Atlassian; IntelliJ IDEA is a trademark of JetBrains s.r.o. This page is not affiliated with or endorsed by Atlassian or JetBrains.
More: Changes missing from a branch · No Merge button, or a greyed-out one · Merge checks · Changes that are already merged · 日本語