Bitbucket Cloud: "Unable to sync branch"
The pull request says the source branch is some commits behind the destination. You press Sync now, and Bitbucket answers "Unable to sync branch" with "feature wasn't synced with main" (your branch names in place of these). Sync now merges one branch into the other, so it writes to the branch, and the usual cause is a branch restriction that doesn't let you push to the source or destination branch (Atlassian's knowledge base, Bitbucket Cloud pull request "Unable to sync branch").
Why it fails
- Sync now writes to the branch. It merges the destination into the source branch with a merge commit, or, if you choose it, rebases the source branch on the destination. Either way it needs write access.
- A branch restriction leaves you out. Under Repository settings > Branch restrictions, a rule for the branch that gives write access only to specific people or groups keeps everyone else from syncing. On the Branches page Sync now is greyed out for them; on the pull request page it can still be pressed, and fails with this message.
- Sometimes on purpose. Bitbucket has no setting to hide Sync now, so Atlassian suggests this same restriction to keep people from syncing (How to disable sync now option). If your team set it, ask who may sync before working around it.
To see which pull requests have fallen behind: Pull Request Inbox for Bitbucket, a plugin for JetBrains IDEs, shows next to each pull request which merge checks block it, a limit on commits behind the destination among them.
Fix it
- Get write access to the branch: a repository admin adds you, or your group, under Write access in the branch restriction.
- Lift the restriction for a moment: Atlassian's other option is to turn the restriction off, sync, and turn it back on.
- Ask someone with write access to press Sync now.
Sync in a clone instead
A sync is a merge you can make yourself. Merge the destination into the source branch:
git fetch origin
git checkout feature
git merge origin/main
git push origin feature
Or rebase, then push with a lease so you don't overwrite a push you haven't seen:
git fetch origin
git checkout feature
git rebase origin/main
git push --force-with-lease origin feature
The push needs write access to the branch too: if the restriction is on it, Bitbucket refuses the push with pre-receive hook declined (what that means). So this helps when the restriction is on the other branch or the sync fails for another reason. If the two branches conflict, resolve the conflict in the clone before you push (resolving merge conflicts).
You have write access and it still fails
In the community question develop wasn't synced with master, the branches had no conflicts and the user had access. Atlassian's answer was to sync in a clone as above, and to find the cause: open the browser's developer tools on the Network tab, press Sync now again, and look for a request that returns a 4xx or 5xx status and what its Response says. Send that to Atlassian support.
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, such as "At most 5 commits behind the target branch" when your repository sets that limit. 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: Merge checks · Bitbucket merge strategies · pre-receive hook declined · 日本語