Check if a pull request is up to date with the target branch

If you want to use github API. Any API that returns the pull request object will have the mergeable_state field in it. If its value is behind, that means the base branch is updated after the pull request is created. ie: pull request branch is out of date.

Here's mergestatestatus explanation

If you are handling webhook responses in Jenkings server, most pull request events like, pull request created, edited, closed or issue_comment events will include the mergeable_state information in the pull request object.


I don't know whether GitHub exposes this information through their API, but you can detect this manually with Git commands. You want to find what is known as the merge base, and ensure that this commit is the same as the tip of master (or whatever your main branch is).

In the form of a bash script, it would look something like this:

if [ $(git merge-base @ master) == $(git rev-parse master) ]
then
  echo "Your branch is up to date."
  exit 0
else
  echo "You need to merge / rebase."
  exit 1
fi

If you include this script as a build step, then the exit values should cause Jenkins to fail the job if necessary.

As mentioned in Dmitry's answer, with newer versions of Git you can use the --is-ancestor flag for git merge-base to simplify it to one command. The script would then look like this:

if git merge-base --is-ancestor master @
then
  echo "Your branch is up to date."
  exit 0
else
  echo "You need to merge / rebase."
  exit 1
fi

There is a modern way of doing so

git merge-base --is-ancestor A B

see git-merge-base