Prevent Merge of Pull Requests Behind Target Branch

A note about out-of-date pull requests

An out-of-date pull request (PR) is based on an earlier version of the target branch. These types of PRs could cause the following problems:

  • Conflicts make the branch not mergeable

  • Merge results are unknown since it has not been built on either the topic or the target branch

Out-of-date PRs inevitably happen because other changes are pushed or merged on to the target branch, like the master branch for example. Then, your PR is behind the target branch. To avoid out-of-date PRs, create the PR from the tip of master. You will have a better chance of merging without conflicts, and a green build from the topic branch means you get a green build after merging because the inputs are the same.

This is an out-of-date PR:

If a pull request is created from topic branch now, the merge result is unknown, and it may be conflicted.


Prevent Merge of Pull Requests Behind Target Branch prevents out-of-date pull requests by preventing PRs that are behind the target branch from merging. If Prevent Merge of Pull Requests Behind Target Branch is applied to your instance, you see this message when trying to merge an out-of-date pull request:

There is no need to lose any additional information that has been added in the description. You can follow these steps to fix the PR:

  1. Decide if you want to rebase or merge the branch. 


    • To rebase, type this in the command prompt window: 

      bash
      git checkout topic git rebase master git push -f
    • To merge, type this in the command prompt window:

      bash
      git checkout topic git merge master git push
  2. Select Create.

    It’s possible that the developer will need to resolve conflicts when rebasing or merging the branch, but they were going to have to be resolved sooner or later, so better to do it sooner.


Another way of ensuring that the merge produces a green build is to build from the automatic merge that Bitbucket does when a pull request is rescoped (i.e. when either branch gets new commits):


git fetch origin  +refs/pull-requests/{id}/*:refs/remotes/origin/pull-requests/{id}/*

Where {id} is the ID of the PR in question. If there is no ref pull-requests/{id}/merge it means there were conflicts. There is interesting further reading here.