Sub topic 9
Multiple Pull Request Templates
This chapter assumes you have already written a single pull request template, as covered in the earlier chapter, and that you know where template files live in a repository. Here you will add several templates to one repository and learn how contributors pick between them.
Why have more than one
One template suits a repository where every change looks alike. Many projects have different kinds of changes. A hotfix needs a short description and a rollback plan. A feature needs design notes and screenshots. A documentation change needs very little. Separate templates let each kind of change ask for the right information.
Where the files go
Put the templates in a dedicated folder. GitHub accepts these locations, all on the default branch:
.github/PULL_REQUEST_TEMPLATE/docs/PULL_REQUEST_TEMPLATE/PULL_REQUEST_TEMPLATE/in the repository root
A typical layout looks like this:
.github/
PULL_REQUEST_TEMPLATE/
feature.md
hotfix.md
docs.mdFile names are yours to choose. Use the .md extension. Short, lowercase names work best because you will type them into URLs.
Writing the templates
Each file is plain Markdown, like the single template. Here is a short hotfix template:
## What is broken?
<!-- Describe the production problem. -->
## The fix
<!-- Explain what this change does. -->
## Rollback plan
<!-- How do we undo this if it goes wrong? -->
- [ ] Tested against the failing case
- [ ] Reviewed by a second personA feature template can ask for different things, such as screenshots and a link to the design.
How contributors choose a template
GitHub does not show a chooser page for pull request templates. This differs from issues, where a chooser appears. When you have several pull request templates, the pull request description starts empty, and the contributor must select a template by name in the URL.
The template query parameter takes the file name, including the extension. Add quick_pull=1 to skip straight to the form on a compare page:
https://github.com/OWNER/REPO/compare/main...my-branch?quick_pull=1&template=hotfix.mdGitHub then opens the new pull request with the contents of hotfix.md in the description.
Because contributors will not guess these URLs, publish them. Link to them from CONTRIBUTING.md, from the README, or from a bot comment. A short list works well:
## Opening a pull request
- [New feature](../../compare?quick_pull=1&template=feature.md)
- [Hotfix](../../compare?quick_pull=1&template=hotfix.md)
- [Docs change](../../compare?quick_pull=1&template=docs.md)The links above use relative paths that resolve against the repository's /compare page. Contributors still need to choose their branches on that page.
Common errors
| Symptom | Likely cause |
|---|---|
| Description is empty | The URL has no `template` parameter, or the folder holds several files and none was named |
| Named template does not load | The file name in the URL does not match exactly, including case and extension |
| Templates are not found at all | The files are not on the default branch, or the folder is misnamed |
| Template ignored in a fork | The templates come from the base repository, so check that repository's default branch |
Summary of the working model
Several templates live in a PULL_REQUEST_TEMPLATE folder on the default branch. GitHub never asks the contributor which one to use. The URL decides, so your job as maintainer includes making the right URLs easy to find.
Exercise
A repository has .github/PULL_REQUEST_TEMPLATE/hotfix.md and .github/PULL_REQUEST_TEMPLATE/feature.md on the default branch. A contributor opens a pull request from the normal "Compare & pull request" button and sees an empty description. Why, and what URL parameter fixes it?
Show answer
With a folder of templates, GitHub does not choose one automatically and shows no chooser. The description stays empty unless the URL names a template. Adding template=hotfix.md (for example ?quick_pull=1&template=hotfix.md on the compare URL) pre-fills the description with that file.
Exercise
You added docs.md to the template folder on a feature branch, but the template does not load when you use template=docs.md. What is the most likely cause?
Show answer
Templates are read from the default branch. Until the file is merged into the default branch, GitHub does not find it.