Checkout bitbucket pull requests locally
Asked Answered
S

11

44

I found this gist, showing how to check out a pull request locally from GitHub.

I'm using bitbucket and I'm looking for a similar function.

Can you help me? Thank you

Sylvia answered 22/9, 2014 at 5:8 Comment(0)
C
34

One may fetch the code from Bitbucket Server's pull requests using:

git fetch origin refs/pull-requests/$PR_NO/from:$LOCAL_BRANCH
Contagion answered 6/11, 2015 at 12:31 Comment(8)
This doesn't work .It gives the following error, fatal: Couldn't find remote ref refs/pull-requests/2/from Unexpected end of command stream.Olander
Are you using forks?Contagion
Do you have your remote origin set to the main repo ? Because If you set it to your fork, then git fetch origin will failContagion
The refspec given by this answer only applies to atlassian's stash, the self-hosted bitbucket of sorts. But it's not working with the publicly open bitbucket. So, I'm sorry but I'm downvoting (and encourage others to downvote as well), as it made me loose a couple of hours thinking it was possible whereas IT IS NOT (at least yet).Medford
@Contagion Thanks for the answer! This works for me on a Bitbucket Server instance (that is, the self-hosted version as indicated by @zmo).Countermand
Stash was named Bitbucket on september 2015, this answer is from november 2015, so it's technically correct. People googling for 'bitbucket fetch pull request' or something like that will find this stackoverflow post, so downvoting is pretty useless.Macon
Works for me 28/11/2018. You need to do a fetch against your remote (eg origin) not a pull. This is against a 'Stash' that is now branded as Bitbucket, so I don't know how this applies to public BitBucketAveril
Can I list all pull requests?Entero
M
24

I found this answer and thought that it was actually possible to fetch refs for a pull request on bitbucket.

But it's not.

The answer for the OP's question is that it is NOT possible: there's been an open feature request issue about it that has been unanswered and unattended for four five SIX SEVEN… nope it's been over

TWELVE years.

The workaround?

You can get the PR as a downloadable .patch file you can download and apply to a new branch you create manually. But you won't easily be able to apply updates.

I figured another way out, which I've implemented in git-repo, so everybody can use it. What I'm doing is use the API to get the PR's remote and branch, and automatically create a new upstream and branch locally. That way you can get updates from the PR poster. The downside is the clutter of git remotes.

edit: I hope this gets done and the feature request is closed. But there has been a solution for this on dedicated bitbucket servers for some time now, but not on the bitbucket.org service. On June 5th, a bitbucket staff member commented on this ticket:

Hi y'all -- thanks again for your feedback and patience on this issue. This feature is still high on the priority list in the backlog. When we have more information to share about the expected delivery of this feature, we will share it here.

Medford answered 3/12, 2016 at 13:20 Comment(9)
I could successfully download a patch running the following command: # Download the patch file. curl -u user:password https://bitbucket.org/api/2.0/repositories/{user}/{repo}/pullrequests/{pull_no}/patch -L -o name.patch # Apply the patch file to your local Git checkout. git apply name.patch Glume
Voting down as it's actually possible now to fetch refs for pull requests on bitbucket (see answer https://mcmap.net/q/195581/-checkout-bitbucket-pull-requests-locally).Camaraderie
from the feature request a member of staff said this month. Your answer works for a dedicated bitbucket server, but not for the bitbucket.org website. I haven't tested yet, but once someone state that it does work, or the linked feature request is closed, I'll update my answer accordingly. Until then, it's sadly not solved yet ¯_(ツ)_/¯Medford
refs/pull-requests/$PR_NO/merge - it works for Butbucket v5.14.1Fulgurating
Hey @zmo, it's almost 9 years already, please update your answer to reflect the actual state of things, as $hitty as they are😫Signally
AFAICT, the issue is still open, so that means it all ¯\_(ツ)_/¯Medford
The issue is that we both are right: some people looking for an answer for on premises bitbucket will be happy with the other answer, while the other people using bitbucket.org won't be happy with my answer. I still don't get why both don't have the same features. 🤷Medford
A patch - is NOT the PR itself, neither are the commits... To "download" or "export" a PR it needs to be material that contains all of the information from the overview page [such as comments] and approvals, logs from any verification steps, status tracking of branch protection rules ALL of it... As far as I have been able to find, there is NO abilit to download a PR.Racemic
the OP was about checking out the code of a PR, the same way github does, and on premises bitbucket does, which is just an alias of the source branch used for the PR, but useful to organise oneself when reviewing code locally. And for some obscure reason, the feature is not imlplemented on bitbucket.org. And the last update is totally WTF, and there's a new ticket about that same feature : jira.atlassian.com/browse/BCLOUD-22185.Medford
K
19

I followed this article Pull request Fetching.

It worked but I found out I just need add one line to the current repo, rather than create a folk repo and an upstream repo. Run this line

git config --add remote.origin.fetch '+refs/pull-requests/*/from:refs/remotes/origin/pr/*'

You can also add it manually to the file .git/config in your project.

Next run git pull you should see a list:

  • [new ref] refs/pull-requests/488/from -> origin/pr/488
  • [new ref] refs/pull-requests/666/from -> origin/pr/666

Then you can run git checkout origin/pr/666 to get the pull request changes.

Kreindler answered 9/8, 2018 at 22:44 Comment(3)
Thanks, this works great on the latest version of BitBucket Server.Pallid
What happens if i do git config --add remote.origin.fetch '+refs/pull-requests/*/from:pr/*' instead? it seems the same to me?Salinas
"pull requeszt changes" is NOT the "pull request"... who approved,who commentewd, when dis reviews start and end..... that is all part of the PR.Racemic
C
17

Fetch/Checkout Pull Requests

This works for bitbucket. Other server could have different refs: (refspecs) or no refs: at all.

First Time

First of all you need to add the pull request refs: of the remote repository. To do that to a repository (e.g. aliased 'upstream'):

git config --add remote.upstream.fetch '+refs/pull-requests/*/from:refs/remotes/upstream/pull-requests/*'

That is, you add the last line on git .config file:

[remote "origin"]
   url = ssh://[email protected]/~user/repository.git
   fetch = +refs/heads/*:refs/remotes/origin/*
   fetch = +refs/pull/*/head:refs/remotes/origin/pull-requests/*

Fetching

Then if you perform the remote fetch you should see the retrieval of (also) all the pull requests:

git fetch upstream

From ssh://git.blablabla.net/somepath/repository
 * [new ref]         refs/pull-requests/1188/from -> upstream/pull-requests/1188
 * [new ref]         refs/pull-requests/1741/from -> upstream/pull-requests/1741
 * [new ref]         refs/pull-requests/2394/from -> upstream/pull-requests/2394

Checking out

Finally you can checkout the pull-request you prefer:

git checkout pull-requests/2723

Successfully tested on dedicated bitbucket server 27/02/19.

Camaraderie answered 27/2, 2019 at 14:21 Comment(5)
Awesome @alex-gidan! Worked perfectly. This would def be my top pick for answer in 2019 with a compatible version of bitbucket. : thumbsup:Fredericafrederich
@Fredericafrederich I agree, lets get this answer straight to the top 8)Tympan
bitbucket server gets all the good features :(Hafner
this is the most straightforward answerJanessa
Is this still valid ?Araxes
E
3

If you use forks probably "origin" is your fork, so first of all you should add the main remote. Take the URL of the main remote clicking the "Clone" button in the repository page the same way you do when you clone a repository

git remote add upstream $UPSTREAM_URL

fetch the pull request

git fetch upstream refs/pull-requests/$PR_NO/from:$LOCAL_BRANCH

checkout the new branch

git checkout $LOCAL_BRANCH
Epineurium answered 9/11, 2022 at 9:11 Comment(0)
B
2

When is not possible to checkout the pull request, a trick is that you can checkout the last commit of that pull request

git checkout <hash code of last commit>
Barbosa answered 23/1, 2019 at 12:0 Comment(1)
This was what I was looking for!Jeanette
F
1

If you are using forked repository and you want to pull PR from original or other repo then use below commands.

 1. git fetch ${URLofOriginalRepo}
    “+refs/pull-requests/*/from:refs/remotes/origin/pr/*” 
 2. git checkout origin/pr/${PR_NUMBER}

URLOfOriginalRepo is the url repository from which you want to pull the PR. This url is the one you use to clone the repo using ssh key.

After running these command you can see the open PR's on this repo. Then pull the one you want.

for eg.

git fetch ssh://hostname.net:8080/repofolder/repo.git.git “+refs/pull-requests//from:refs/remotes/origin/pr/” && git fetch origin/pr/854

Forcemeat answered 12/10, 2021 at 21:52 Comment(0)
Y
0

It seems the easiest way to do this is still to get a patch of the pull request. Based on this question's answer, Alexandre's comment is still the only way to do this. It uses this BitBucket API call.

I used the following bash script:

USER=username
PASSWORD=password
REPO=repo-name
PULL_NO=42
OUTPUT_FILE=output.patch

# Add -i to include the HTTP-header in the output for debugging
curl -u $USER:$PASSWORD https://bitbucket.org/api/2.0/repositories/$USER/$REPO/pullrequests/$PULL_NO/patch -L -o $OUTPUT_FILE

Save that to a file called pull-patch.sh and fill in the environment variables with your account details. The script requires that you have curl installed (e.g. sudo apt install curl). Then run:

chmod +x pull-patch.sh
./pull-patch.sh

And a file called output.patch should be created from the pull request.

Yadirayaeger answered 7/7, 2018 at 13:12 Comment(0)
K
0

In bitbucket what you can do is

git config remote.origin.fetch "+refs/heads/:refs/remotes/origin/"

and then

git fetch

after that you can checkout to the branch you want to

git checkout BRANCH_NAME

P.S: Hopefully bitbucket will sort this out https://jira.atlassian.com/browse/BCLOUD-5814

Kellogg answered 8/10, 2019 at 9:4 Comment(0)
L
0

I found this difficult in bit bucket, so, I tried a different approach. If a person give a pull request to my repo in bitbucket, i set his(bill) origin by him name(bill) . then to this -

git checkout -b bill-auth bill/bill-auth 

Here bill is that contributer repo origin / link , then bill-auth is his branch name.

Here I am creating a branch same name as his(bill) feature branch.

Linn answered 14/6, 2020 at 18:35 Comment(0)
F
0

git fetch origin refs/pull-requests/$PR_NO/merge - it works for Butbucket v5.14.1

Fulgurating answered 18/6, 2020 at 9:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.