Sub topic 6
Linking Pull Requests and Closing Issues Automatically
This chapter assumes you can open a pull request, know what the default branch is, and have created and labeled issues. It covers how a pull request connects to an issue and how that connection closes the issue for you.
Why link pull requests to issues
An issue says what needs to change. A pull request is the change. Linking them gives reviewers the context behind the code. It also gives the issue a history of the work done on it. When the pull request merges, GitHub can close the issue, so the board stays accurate without manual cleanup.
Closing keywords
Put a closing keyword before an issue reference in the pull request description. You can also use it in a commit message. These keywords work:
- close, closes, closed
- fix, fixes, fixed
- resolve, resolves, resolved
Fixes #42
This change validates the date field before saving.
Related to #51.When this pull request merges into the default branch, issue #42 closes. "Related to #51" has no closing keyword, so it only creates a reference. Issue #51 shows a link to the pull request but stays open.
What happens, step by step
The link appears as soon as you save the description. The issue closes only at merge time. If you close the pull request without merging, the issue stays open.
Rules that cause common errors
- The target branch matters. Keywords in a pull request close issues only when it merges into the default branch, usually
main. A pull request merged into a feature branch or release branch does not close anything. - Each issue needs its own keyword.
Fixes #42, #43closes only #42. WriteFixes #42, fixes #43. - Cross-repository issues need the full reference. Use
Fixes owner/repo/#42orFixes owner/repo#42. You need write access to that repository for the link to work. - Keywords in comments do not count. Only the pull request description and commit messages trigger closing. A comment saying "fixes #42" creates a plain reference.
- Punctuation can break the match. Write
Fixes #42with a space. Avoid bold or other formatting around the keyword.
Linking manually
You can also link without a keyword. In the pull request's sidebar, open Development and choose the issue. This is useful when the work is related but you do not want the merge to close the issue. It also works when you forgot the keyword. A link made this way closes the issue on merge, as with a keyword. Removing the link from the sidebar stops that.
A keyword link made in the description cannot be removed from the sidebar. Edit the description instead.
Linking from the issue side
From an issue, the Development section lets you create a branch for that issue. The branch is linked to the issue automatically. A pull request opened from that branch is linked as well, so it closes the issue when merged.
Verifying and reopening
After a merge, the issue shows a "closed as completed" event that points to the pull request. If an issue closed by mistake, reopen it from the issue page. A reopened issue stays open even if the old link remains.
Exercise
A pull request targets the branch release/2.0, not main, and its description says "Fixes #42". The pull request merges. Is issue #42 closed, and what would you change?
Show answer
No. Closing keywords only act when the pull request merges into the default branch. Either retarget the pull request to main, close the issue manually after the merge, or link the issue and close it yourself once the work reaches main.
Exercise
Rewrite "Closes #10, #11, #12" so that all three issues close on merge.
Show answer
Closes #10, closes #11, closes #12