414
votes

I'm not sure, but I have a vague memory of creating a github pull request with "Issue 4" or something in the title, and it automatically attached itself to Issue 4 in the project that I was submitting it to. I tried it again recently and it didn't work -- it just created a brand new issue instead. I don't see any options like "Attach to issue" on the new pull request page, nor "Open a new pull request for this issue" on the issue page. Is there any way to do this, to help project owners keep their Issues page clean and avoid duplication?

Edit: To clarify, I know that creating a pull request always creates a new issue. I would like to instead attach the pull request to an existing issue.

10
I believe my answer express the fact that the feature you want ("attach a pull request to an existing issue") might not be there yet.VonC
It does (and that is in fact confirmed by this tweet), but it also made me realize my question could have been clearer.MatrixFrog
I hope that feature is high on github priority list, coz the code bears out there would love it!flq
The correct answer ought to be changed to masukomi's, now that the "fixes #1" method available. No need to go through the API.Edward Anderson
I still cannot find a way to attach a pull request to an existing issue. Have I missed something? The answers in this thread seems to suggest this capability does exist, but I cannot find it (it always makes a new issue).Kevin Jalbert

10 Answers

246
votes

The "hub" project can do this:

https://github.com/defunkt/hub

In the repository and branch that you want to send a pull request from:

$ hub pull-request -i 4

This uses the GitHub API, and attaches a pull request for the current branch to the existing issue number 4.


EDIT: Comment by @atomicules: To expand on the answer by @MichaelMior a full example is:

$ hub pull-request -i 4 -b USERNAME_OF_UPSTREAM_OWNER:UPSTREAM_BRANCH -h YOUR_USERNAME:YOUR_BRANCH URL_TO_ISSUE
248
votes

Adding a pull request to an existing upstream issue is easy assuming you forked using the normal github means.

Simply reference the issue in your commit message using any of the supported keywords:

  • close
  • closes
  • closed
  • fix
  • fixes
  • fixed
  • resolve
  • resolves
  • resolved

For example: "this commit fixes #116"

The text referencing the issue does not need to appear in the subject line of your commit.

Push your commit to your github repo and the pull request will be automatically appended to the issue.

Note: While it is not required, it is strongly recommended that you commit anything that will be part of a pull request to a separate branch specific to that issue, because future commits on that branch will be appended to the pull request (automatically by github). So, if you didn't make a separate branch, left it on master, and then kept developing, then all your unrelated commits to master would get appended to your pull request.

144
votes

You can create a Pull Request from an existing Issue with the Pull Request API:

$ curl --user "smparkes" \
       --request POST \
       --data '{"issue": 15, "head": "smparkes:synchrony", "base": "master"}' \
       https://api.github.com/repos/technoweenie/faraday/pulls

This creates a pull request:

  • ask technoweenie at project faraday (https://api.github.com/repos/technoweenie/faraday/pulls)
  • to pull from the synchrony branch in smparkes' fork ("head": "smparkes:synchrony")
  • to the master branch in technoweenie's fork ("base": "master")
  • and attach the pull request to issue 15 ("issue": 15)
  • with the pull request author smparkes (--user "smparkes")
  • you will be prompted for your GitHub password
17
votes

Another possible tool is the Issue2Pr website which turns your issues into Pull Requests.

It's very simple and effective!

enter image description here

Resources:

10
votes

This other answer explains how to use cURL (curl) to create a Pull Request from an Issue through the GitHub API. Here’s how to do it using HTTPie (http), which produces an easier-to-read and easier-to-edit command:

$ http --auth "<your-GitHub-username>" \
       POST \
       https://api.github.com/repos/<issue-repo-owner>/<issue-repo-name>/pulls \
       issue=<issue-number> head=<your-GitHub-username>:<your-fork-branch-name> base=<issue-repo-branch-name>

Then type your GitHub password when prompted.

Explained example

You have logged into GitHub with username smparkes and password hunter2. You saw technoweenie’s repo faraday, thought of something that should be changed, and made an Issue on that repo for it, Issue #15. Later, you find that nobody else has made your proposed change, and you also have some time to do it yourself. You fork faraday to your own account, then write your changes and push them to your fork under a branch named synchrony. You think technoweenie should pull those changes to the master branch of his repo. This is the command you would write to convert your previous Issue into a Pull Request for this situation:

$ http --auth "smparkes" \
       POST \
       https://api.github.com/repos/technoweenie/faraday/pulls \
       issue=15 head=smparkes:synchrony base=master
http: password for [email protected]: hunter2

Now Issue #15 is a Pull Request.

3
votes

in case you use 2-factor-auth with github you'll need to provide the authtoken as header in the request:

curl -u "<your_username>:<your_pw>" \
     --header 'X-GitHub-OTP: <your_authtoken>' \
     --request POST \
     --data '{"issue":"<issue_nr>", "head":"<your_username>:<your_forks_branchname>", "base":"<upstream_branch>"}' \
     https://api.github.com/repos/<upstream_user>/<upstream_repo>/pulls
2
votes

Instead of doing that on the client side (with hub, as in Christian Oudard answer), you now (February 2020) can do it on the server side (github.com)

See "View and link issues and pull requests from the sidebar "

You can now link issues and pull requests via the sidebar in their respective pages. Connections made here will automatically close issues once a linked pull request is merged.

Documentation: https://help.github.com/assets/images/help/pull_requests/link-issue-drop-down.png

And there is a search API with that feature.

Find all the open issues in a repository that have closing pull requests references with the linked:pr search qualifier.
Similarly, locate all the pull requests in a repository that are missing a supporting issue with -linked:issue.

1
votes

You may also use Gub to submit pull requests for your issue.

It also helps you use a proper fork/pull-request style.

Edit: 10/5/2013

To get Gub to submit pull-request for issue #123, you need to run the following:

$ gub start 123

This will create a new branch issue-123. Once you're done working on the issue, execute:

$ gub finish

Voila!

Note: I am the author of Gub gem.

0
votes

Using the git-hub tool, you could do this with:

$> git hub pull attach 123

This would convert issue #123 into pull request #123, thus maintaining all discussion about the issue in a single location.

0
votes

If you have 2FA enabled, you can use pass the token with HTTPie:

http POST \
    https://api.github.com/repos/<repo-owner>/<repo-name>/pulls \
    issue=2 head=issue_2 base=master
    "Authorization:token PUTAUTHTOKENHERE"

This will use the branch issue_2 to convert issue #2 into a pull request.