How can I filter all GitHub pull requests for a specific target branch

Viewed 33478

I'm working on a GitHub repo with lots of branches and pull requests.

Let's say, for example, that I have the following pull requests:

  • a to branch X
  • b to branch X
  • c to branch Y
  • d to branch X
  • e to branch Y.

Is there a way to find all the pull requests that are targeted for branch X (i.e. a -> X, b -> X, d -> X)?

4 Answers

How to search for PRs by "from (head) branch", by "to (base) branch", and by author, including doing this using custom Chrome search engines you can quickly trigger in your search bar:

Note: the below searches for PRs (Pull Requests) on GitHub are intended to be done in the PR search bar here (https://github.com --> "Pull requests" at top) (direct link: https://github.com/pulls), not in the generic GitHub search bar at the top-left of nearly any GitHub page, although they might work there too.

See also my answer here: Can I search github labels with logical operator OR?

In GitHub lingo, the branch you are merging FROM is the head branch, and the branch you are merging TO is the base branch. See here: https://docs.github.com/en/github/searching-for-information-on-github/searching-issues-and-pull-requests#search-by-branch-name. I'm not sure why they didn't choose from_branch and to_branch instead, as that would have been easier to remember.

1. Find all PRs FROM my_branch by using the search string head:my_branch:

is:open is:pr archived:false head:my_branch

Optionally, specify a repository too:

is:open is:pr archived:false repo:some_username/some_repository head:my_branch

2. Find all PRs merging inTO my_branch by using the search string base:my_branch:

is:open is:pr archived:false base:my_branch

Optionally, specify a repository too:

is:open is:pr archived:false repo:some_username/some_repository base:my_branch

3. References:

  1. From the official GitHub documentation: https://docs.github.com/en/github/searching-for-information-on-github/searching-issues-and-pull-requests#search-by-branch-name:

    Search by branch name

    You can filter pull requests based on the branch they came from (the "head" branch) or the branch they are merging into (the "base" branch).

    Qualifier               Example  
    ---------------------   -------------------------------------------------
    `head:HEAD_BRANCH`      `head:change is:closed is:unmerged` matches pull 
                            requests opened from branch names beginning with 
                            the word "change" that are closed.
    
    `base:BASE_BRANCH`      `base:gh-pages` matches pull requests that are 
                            being merged into the gh-pages branch.
    
  2. @Andor Dávid's answer here

4. GOING FURTHER: Add custom Google Chrome search engines for GitHub searches

For those who don't know, the Chrome browser allows you to create custom search engines. If I go to my browser search bar and type gto followed by Space or Tab, then type in my branch name, my_branch, I see this. I am literally searching for all open PRs merging TO branch my_branch, using my custom search engine triggered by the gto shortcut I set up:

enter image description here

Here's how to configure those:

In Chrome, click on 3 dots in top-right --> Settings --> Search engine (in left pane) --> "Manage search engines" --> under "Other search engines", click the "Add" button. Set it up like this: enter image description here

Click "Save" when done. Here are 3 of my favorites:

  1. Search for GitHub PRs merging TO a given branch:
    1. Search engine: GitHub PRs merging TO branch
    2. Keyword: gto
    3. URL with %s in place of query: https://github.com/pulls?q=is%3Aopen+is%3Apr+archived%3Afalse+base%3A%s
  2. Search for GitHub PRs merging FROM a given branch:
    1. Search engine: GitHub PRs merging FROM branch
    2. Keyword: gfrom
    3. URL with %s in place of query: https://github.com/pulls?q=is%3Aopen+is%3Apr+archived%3Afalse+head%3A%s
  3. Search for GitHub PRs BY a certain author:
    1. Search engine: GitHub PRs BY this User
    2. Keyword: gby
    3. URL with %s in place of query: https://github.com/pulls?q=is%3Aopen+is%3Apr+author%3A%s+archived%3Afalse+

The way you figure out what to add for the URL search string is actually quite simple: you just go to GitHub and do a search manually using GitHub's tools, then you copy and paste the URL, replacing the search string you'd like to search for with %s in the custom Chrome Search engine URL. That's it! This process works for any and all websites which store their search variables into the URL once you do a custom search on their website, which is many, if not most, websites with search capability.

Keywords: Google chrome / browser custom search engines for GitHub PRs

Now that the GitHub CLI is available, here's how you would do it for a given repository.

You can install the gh CLI following the appropriate instructions for your OS and preferred package manager.

I use the cli/cli repo to demonstrate:

gh pr list --base trunk -R cli/cli

Output:

#3045  [hackday] mergeconflict                                                  hackday2102
#3044  Isolate test suite from environment variables                            env-tests
#3042  Remove functionality to add, view and edit binary files in gists         g14a:bug/gist-binary-files
#3023  Issue/pr create: exit with nonzero status code when "Cancel" was chosen  cancel-error-status
#3018  Add `pr create --body-file` flag                                         castaneai:pr-create-body-file
#3010  Add `api --cache` flag                                                   api-cache
#3008  Add interactive select in gist view                                      ganboonhong:interactive-gist-view
#2997  Feature/add files to gist                                                g14a:feature/add-files-to-gist
#2991  Repo create tweaks                                                       repo-create-prompt-change
#2953  Add `repo list` command                                                  cristiand391:add-repo-list
#2923  [WIP] first round of actions support                                     actions
#2728  Better completions                                                       rsteube:better-completions
#2261  pr status: show number of approvals                                      despreston:des/approval-count
#2160  Sign Windows .exes in a post-build hook                                  mbpreble:sign-windows-executables
#2080  store gh config in %APPDATA% on windows os                               RozzaysRed:appdata_configPath
Related