Skip to content

Instantly share code, notes, and snippets.



Forked from piscisaureus/
Last active Dec 15, 2017
What would you like to do?
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 = git@stash.internal: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 = git@stash.internal: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'

This comment has been minimized.

Copy link

@chinchang chinchang commented Oct 7, 2015

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


This comment has been minimized.

Copy link

@vid-n3t 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

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