Skip to main content
← All posts

How GitHub decides a pull request is linked to an issue

When you open an issue on GitHub, the Development section in the sidebar lists the pull requests linked to it. That list drives a lot: it is what closes the issue when the pull request merges, and it is what most tools, ours included, read to put a pull request next to its issue. So it is worth knowing exactly what puts a pull request in that list, because it is narrower than it looks.

We started linking every pull request in our own repository to a ticket this summer, and we got this wrong in several ways before we got it right.

Two ways in, and only two

GitHub links a pull request to an issue in one of two ways.

A closing keyword in the pull request's description. The keywords are close, closes, closed, fix, fixes, fixed, resolve, resolves and resolved, followed by the issue: Closes #10 in the same repository, or Fixes octo-org/octo-repo#100 for another one. Colons and capitals are fine (Fixes: #10). For several issues, repeat the keyword for each one.

A manual link from the sidebar. Anyone with write access can link a pull request to an issue from the pull request's sidebar or from the issue's, up to ten issues per pull request.

Everything else is a mention, not a link.

"Refs #12" is a mention

This was our first mistake. Refs #12, See #12, Part of #12 and a bare #12 all turn into a clickable link in the description, and GitHub adds a back-reference to the issue's timeline, so it looks connected. But none of them is a closing keyword. The pull request doesn't appear under Development, merging it closes nothing, and anything that reads GitHub's linked pull requests will not see it.

If you want the link, use a closing keyword, or link by hand. Either way, GitHub's documentation says that merging a linked pull request into the default branch closes its linked issue, so there is no built-in way to link a pull request without closing the issue when it merges. If the pull request is one step of several, plan for that: split the issue, or reopen it.

The keyword only counts on the default branch

GitHub's documentation is explicit: the pull request must target the default branch. If it targets any other branch, the keywords are ignored, no link is made, and merging it does nothing to the issue.

That catches stacked pull requests. Open a second pull request on top of the first, write Closes #14 in it, and GitHub shows no link at all, because its base is the first pull request's branch rather than main. GitHub's docs don't say whether the link appears once you retarget the pull request at main, so check the issue's Development section after retargeting.

Keywords in a commit message are a separate route. The issue still closes when the commit lands on the default branch, but the pull request carrying the commit is not listed as linked.

Put the keyword at the top

This one is our own observation, not anything GitHub documents.

We had a pull request with a very large description, about 65.8 KB of generated output, with Closes #N appended at the end. GitHub stored the description and rendered it, keyword included. It never linked the issue. Moving the keyword to the top of the description fixed it.

We have not found where the cut-off is, or whether there is a fixed one, so treat it as a habit rather than a rule: closing keywords go in the first lines of the description, ahead of logs, checklists or anything generated. And check the result, rather than the text you wrote. The issue's Development section is the quick check; in the GraphQL API it is the pull request's closingIssuesReferences.

Dependabot does not keep your edits

If you link Dependabot's pull requests to tickets, you will meet this. When a newer version of a dependency comes out while its pull request is still open, Dependabot closes that pull request and opens a fresh one for the newer version. The new pull request's description is Dependabot's own. The Closes #N you added by hand is not carried over, so the ticket silently loses its pull request. That is what we saw on our own repository; GitHub's docs don't describe it.

Links are not self-maintaining. Anything that recreates a pull request — a bot, a script, closing and reopening from a new branch — needs the keyword again.

Keywords in code don't count

In our testing, a Fixes #12 inside a fenced code block, an indented code block or inline code does not link. A keyword in a quoted line (> Fixes #12) does. That is mostly what you would want, since pasted logs and code samples shouldn't close issues, but it means quoting an old description can create a link you didn't mean.

"Relates to" is for issues, not pull requests

On 25 September GitHub made its Relates to relationship generally available, after a public preview in August. It connects two issues without saying that one blocks, duplicates, contains or closes the other, and it works across the REST and GraphQL APIs, webhooks, timeline events and search.

It is a welcome addition, and the natural home for what most of us meant by Refs #12 between two issues. It is not a way to link a pull request to an issue. For that, the two routes above are still the only ones.

Where GitGumbo fits

GitGumbo puts linked pull requests on each issue's card, including stacked ones that GitHub's own sidebar leaves out. A bare mention still doesn't link, by design: it would put pull requests on cards they never meant to close.