Glen Cooney
Glen Cooney

Reputation: 51

Git: Getting a Branch Name from a PR

I went ahead and followed this guide to allow me to checkout branches by their PR number rather than the branch name.

However, I am wondering if the reverse is possible, allowing me to get the branch name given a PR number from the command line. Is it doable?

Upvotes: 1

Views: 6419

Answers (2)

Glen Cooney
Glen Cooney

Reputation: 51

Turns out not through github directly, but can be done through the very powerful hub CLI tool created by Github themselves.

https://github.com/github/hub

With a combination of hub and parsing tools, you can checkout PR number 6350 like this:

git checkout $(hub pr list -f "%I|%H|%n" | grep "6350.*" | cut -d "|" -f2)

Upvotes: 1

torek
torek

Reputation: 488103

The short answer is no: there's no mapping (offered by GitHub by a standard Git-oriented interface, at least) showing the target branch of a pull request. However, all hope is not lost! The GitHub Pull Request API can give you the status of an existing pull request: GET /repos/:owner/:repo/pulls/:number returns a rather large JSON blob that has the required information.

Note that parsing this JSON blob requires additional tools.

Upvotes: 2

Related Questions