GitHub PR Graph Generator
A simple Python script to visualize the branch relationships between open pull requests in a GitHub repository.
Example Insight
Before we go any further, let's look at some example output of this script to see what kind of insights it provides.
This example demonstrates a fairly complex PR dependency graph with:
- Multiple primary branches (
main,client-1/main,client-2/main) highlighted in blue - Stacked feature branches where PRs build on top of each other
- Release branches that aggregate multiple features
- Long dependency chains (like the data migration work flowing
through
project/data-migration) - Parallel work streams merging into different targets
The visualization makes it easy to understand which PRs are blocking others and plan merge strategies accordingly.
Installation
Prerequisites
You'll need Python and the requests library:
Installing Graphviz (Optional)
If you want to generate PNG or SVG images locally, install Graphviz:
macOS:
Debian/Ubuntu:
sudo apt-get install graphviz
Don't want to install Graphviz? No problem! You can paste the
contents of the generated .dot file into
GraphvizOnline to
visualize it in your browser.
Getting the script
-
Download the
generate_pr_graph.pyscript from this repository. -
Make it executable:
chmod +x generate_pr_graph.py
-
(Optional) Move it to a location in your
PATHfor easy access:mv generate_pr_graph.py /usr/local/bin/
GitHub Authentication (Optional)
For public repositories, no authentication is required. For private repositories, you'll need a GitHub personal access token:
- Go to the GitHub section for personal access tokens
- Generate a new token (classic) with
reposcope - Set it as an environment variable:
export GITHUB_TOKEN=ghp_your_token_here
Usage
Basic usage
For public repositories:
./generate_pr_graph.py owner/repo
For example:
./generate_pr_graph.py microsoft/vscode ./generate_pr_graph.py kubernetes/kubernetes
Private repositories
Set your GitHub token first:
export GITHUB_TOKEN=ghp_your_token_here
./generate_pr_graph.py mycompany/private-repoSetting a default repository
If you work with the same repository frequently, set a default:
export GITHUB_REPO=mycompany/private-repo
./generate_pr_graph.pyShow all branches (including orphans)
By default, the visualization only shows branches involved in open PRs. To include branches without PRs (useful for seeing work that hasn't been proposed for release yet):
./generate_pr_graph.py owner/repo --show-all-branches
By default, orphan branches appear in white with italic text, while
branches with PRs appear in light blue. Primary branches (like main)
are highlighted in lavender.
Find stale branches
Sometimes, you have branches that linger even though they don't have any commits ahead of their primary branch. These branches are typically already merged or abandoned.
Either way, these are candidates for deletion.
./generate_pr_graph.py owner/repo --find-stale-branches
The script will list potentially stale branches along with git commands to delete them locally and remotely.
Tip: You can combine both flags:
./generate_pr_graph.py owner/repo --show-all-branches --find-stale-branches
Output
The script generates date-stamped files in organized folders:
dot/pr_graph_2025-10-30.dot # Graphviz source file
png/pr_graph_2025-10-30.png # Generated image (after running dot command)
To generate an image from the .dot file:
dot -Tpng dot/pr_graph_2025-10-30.dot -o png/pr_graph_2025-10-30.png dot -Tsvg dot/pr_graph_2025-10-30.dot -o svg/pr_graph_2025-10-30.svg
The script will print these commands for you after generating the .dot file.
Configuration
You can customize the script by editing these constants at the top of
generate_pr_graph.py:
MAX_TITLE_LENGTH: Maximum length for PR titles in graph labels (default: 50)PRIMARY_BRANCH_NAMES: Branch names to highlight in the graph (default:["main", "master", "develop"])PRIMARY_BRANCH_COLOR,PR_BRANCH_COLOR,ORPHAN_BRANCH_COLOR: Colors for different branch types (default:"lavender","aliceblue","white"). See Graphviz color names for available colors.
Contributing
If you find a bug or have a feature request, please open an issue. Pull requests are welcome!
Copyright and license
Copyright (c) 2025 Harish Narayanan.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
