Check out a Bitbucket pull request locally
To build, run or debug a pull request before you approve it, you need its commits in your clone. On GitHub a pull request ref does that; on Bitbucket it depends on whether you use Data Center or Cloud. The Stack Overflow question asking how has been opened about 38,000 times, and the one asking for a pull request as a patch about 22,000 times.
Bitbucket Data Center: refs/pull-requests
Data Center (formerly Stash) keeps each pull request's source in the destination repository as refs/pull-requests/ID/from, so you can fetch it even when it comes from a fork you cannot read:
git fetch origin refs/pull-requests/12/from:pr-12 git switch pr-12
To get all of them as remote branches, Atlassian's article Pull Request Proficiency: Fetching Abilities Unlocked adds a refspec to the remote:
git config --add remote.origin.fetch '+refs/pull-requests/*/from:refs/remotes/origin/pr/*' git fetch origin # origin/pr/12, origin/pr/13, ... git switch -c pr-12 origin/pr/12
When the author pushes again, fetch again: origin/pr/12 moves, your pr-12 does not.
Bitbucket Cloud: fetch the branch
Bitbucket Cloud does not publish refs for pull requests (one answer counts the feature request for them as open for over twelve years). Fetch the source branch, whose name is at the top of the pull request:
# source branch in the same repository git fetch origin feature/login git switch feature/login # source branch in a fork you can read git fetch https://bitbucket.org/contoso-dev/web-app.git feature/login git switch -c pr-12 FETCH_HEAD
Bitbucket Cloud: the pull request as a patch or a diff
When you cannot read the fork, or only want the change, the pull requests REST API has /pullrequests/ID/patch and /pullrequests/ID/diff. Both answer with a 302 redirect to the repository's patch or diff for the pull request, so curl needs -L:
curl -L -u 'your-atlassian-email:your-api-token' -o pr-12.patch \ https://api.bitbucket.org/2.0/repositories/contoso/web-app/pullrequests/12/patch git switch -c pr-12 origin/main git am pr-12.patch
- patch is a series of the source branch's commits with their headers (author, message), for
git am, which recreates the commits. diff is only the change, forgit apply; the accepted answer to the patch question, about a private fork, uses it. - If
git amstops because the destination branch has moved, rungit am --abortand apply the diff withgit apply --3way, which falls back to a three-way merge and can leave conflict markers in the files to resolve. - With an API token, the user name is your Atlassian account email, and the token needs to read pull requests; a token without it gets "Your credentials lack one or more required privilege scopes". The repository patch's reference documents a 555 response when the diff is too large and times out (diffs that are too large).
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, from Bitbucket Cloud and, in beta, from Bitbucket Data Center 8.9 or later, and checks out a pull request's source branch from there. On a pull request with merge conflicts, Resolve in the IDE fetches, checks out the source branch and merges the destination branch into it. The review inbox, diffs, discussions and merge checks are free; approving, commenting and merging are Pro (US$1.50 a month, with a 30-day trial).
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: Restore a deleted branch · Bitbucket pull requests from the command line · Bitbucket pull request merge conflicts · Bitbucket pull request plugins for IntelliJ IDEA, compared · 日本語