Skip to content

Instantly share code, notes, and snippets.

@hongymagic
Forked from piscisaureus/pr.md
Last active December 15, 2017 09:50
Show Gist options
  • Save hongymagic/6339056 to your computer and use it in GitHub Desktop.
Save hongymagic/6339056 to your computer and use it in GitHub Desktop.
Checkout pull-requests from Atlassian Stash without adding remote

Locate the section for your stash remote in the .git/config file. It looks like this:

[remote "origin"]
	fetch = +refs/heads/*:refs/remotes/origin/*
	url = [email protected]:private/repository.git

Now add the line fetch = +refs/pull-requests/*:refs/remotes/origin/pull-requests/* to this section. Obviously, change the stash url to match your project's URL. It ends up looking like this:

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

Now fetch all the pull requests:

$ git fetch origin
From stash.internal:private/repository
 * [new ref]         refs/pull-requests/1000/from -> origin/pull-requests/1000
 * [new ref]         refs/pull-requests/1001/from -> origin/pull-requests/1001
 * [new ref]         refs/pull-requests/1002/from -> origin/pull-requests/1002
 * [new ref]         refs/pull-requests/1003/from -> origin/pull-requests/1003
...

To check out a particular pull request:

$ git checkout pull-requests/1000/from
Branch pull-requests/1000/from set up to track remote branch pull-requests/1000/from from origin.
Switched to a new branch 'pull-requests/1000/from'
@chinchang
Copy link

Would it be possible to get actual branch name instead of /1000/from?

@vid-n3t
Copy link

vid-n3t commented Feb 9, 2016

I'm able to create a branch from the fetched pull request this way, but you still need to know the pull request id:

git fetch origin pull-requests/2/from:pullrequest
git checkout pullrequest

Where 2 is the pull-request id from the URL
https://myStashURL/projects/PROJECT_NAME/repos/REPO/pull-requests/2/overview

I can still update the .git/config (as above) but it's unneeded if you know the id of the pull request (say from an email notification).
Also; here's the one-liner I use to update the .git/config as needed:
git config --add remote.origin.fetch '+refs/pull-requests/:refs/remotes/origin/pull-requests/'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment