421

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.

Tyler
  • 21,762
  • 11
  • 61
  • 90
  • 1
    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 Dec 25 '10 at 01:19
  • It does (and that is in fact confirmed by [this tweet](http://twitter.com/GitHubHelp/status/18475695785115648)), but it also made me realize my question could have been clearer. – Tyler Dec 25 '10 at 01:24
  • I hope that feature is high on github priority list, coz the code bears out there would love it! – flq Jan 03 '11 at 12:53
  • 2
    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 Sep 15 '11 at 18:35
  • 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 Oct 05 '11 at 22:02
  • The correct answer should be changed to Christian Oudard's answer. While still valuable info, masukomi's answer doesn't actually submit a pull request. It only associates a commit with an existing issue. GitHub has the ability to show whether code is attached to an issue, and this is only done with an associated pull request. – Ryan McGeary Nov 18 '11 at 17:01
  • @RyanMcGeary okay I guess you're right. Switched. – Tyler Nov 18 '11 at 21:20

10 Answers10

256

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.

Bob
  • 15,441
  • 3
  • 26
  • 42
masukomi
  • 10,313
  • 10
  • 40
  • 49
  • 35
    "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" -- very good point. That happened to me once and it was quite surprising. – Tyler Aug 25 '11 at 16:46
  • 9
    This does not solve the problem of turning an issue into a pull request unfortunately. Any discussion that was had in the issue does not get transferred to the pull request... which is unfortunate for several use-cases. I wish Github would just give some granular control over how pull-reqs work in the repo settings. – Alex Waters Feb 16 '12 at 15:21
  • 1
    I'm not sure why you need an issue turned into a pull request. Why isn't having the pull-request mentioned in the issue's conversation sufficient? This would happen automatically by referencing the # in the pull req. You have a covo thread. Then you have a thread with the relevant pull req embedded in it. Sounds perfect. – masukomi Feb 16 '12 at 18:59
  • 1
    @masukomi A pull request is easier for the project maintainer to resolve – they can accept and merge the changes with the click of a button. To pull a change in a fork without using a pull request, you would have to add the fork as a remote, fetch their changes, and merge them yourself. – Rory O'Kane Aug 13 '12 at 19:45
  • 2
    I think you missed my point Rory. If you create a pull request and mention it in the issue (as I suggested), then the two are connected, and you still can click a button to get the changes. – masukomi Aug 14 '12 at 03:21
  • Reference from GitHub help: https://help.github.com/articles/closing-issues-via-commit-messages – Bob Feb 23 '14 at 14:32
  • 2
    This doesn't help when the pull request is an on going thing. Our workflow is to create issues for ideas, and then pull requests from feature branches once we begin work on those ideas. Closing the issue using a commit in the pull request means we lose the previous discussion that the issue contained, which often includes hashing out whatever feature/fix/refactor the issue addresses. What is really needed is a way to straight up turn an issue in to a pull request once work on the issue has begun. – Daniel Bingham Jul 24 '14 at 21:35
245

The hub project can do this.

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
Jaroslav Bezděk
  • 6,967
  • 6
  • 29
  • 46
Christian Oudard
  • 48,140
  • 25
  • 66
  • 69
  • 14
    `brew install hub` to install with homebrew – gcamp Jul 29 '12 at 06:01
  • 11
    This doesn't work for me. Says Error creating pull request: Unprocessable Entity (HTTP 422) – Rubycut Aug 03 '12 at 09:39
  • 11
    @Rubycut I had the same problem. Instead I did `hub pull-request URL_TO_ISSUE`, then it worked for me. I wonder if `-i ISSUE_NUMBER` only works if the issue is in the same repository (i.e., not a fork) – Michael Mior Sep 16 '12 at 04:10
  • 30
    To expand on the answer by @MichaelMior a full example is: `hub pull-request -b USERNAME_OF_UPSTREAM_OWNER:UPSTREAM_BRANCH -h YOUR_USERNAME:YOUR_BRANCH URL_TO_ISSUE` – atomicules Nov 21 '12 at 16:18
  • 1
    @atomicules Actually, all I had to do was exactly my command above. This assumes you are sending a pull request for the current branch to the default branch of the upstream repo. – Michael Mior Nov 21 '12 at 18:35
  • 4
    Note this only works on issues you created: https://github.com/defunkt/hub/issues/189#issuecomment-6353354 – Zach Jan 11 '13 at 08:17
  • 3
    I forked a repo. I created an issue on the upstream repo. I commited a change to my forked repo. I tried to add my change to the issue I created. `-i xx` doesn't work for me. @MichaelMior 's `URL_TO_ISSUE` works. – Haozhun Feb 01 '13 at 16:17
  • @atomicules can we have that as an accepted answer instead of just a comment? – chakrit Aug 21 '13 at 07:54
  • @chakrit someone could edit the answer to include that info if they deemed it appropriate. – atomicules Aug 27 '13 at 22:34
  • @atomicules just did that. But honestly I'd rather the reps goes to the one with the most correct answer than the one who simply posted first. – chakrit Aug 28 '13 at 06:06
  • 1
    I had to do `git push -u origin featurebranch`, then `hub pull-request -i 4` worked. ([reference](https://github.com/github/hub/issues/189#issuecomment-6140154)) – dentarg Sep 14 '13 at 16:46
  • 3
    `Warning: Issue to pull request conversion is deprecated and might not work in the future.` And indeed, it doesn't work. – r3m0t Mar 04 '14 at 16:42
  • 1
    It worked for me, but that deprecation warning still means it's doing away. Does this mean we're losing this functionality or is github creating some other way to turn issues into pull requests? – Daniel Bingham Jul 17 '14 at 02:10
  • Neither -i or the full issue URL works for me, getting HTTP 422 in both cases. – Zitrax Jun 08 '15 at 10:02
  • Same for me, getting HTTP 422 and "Not allowed to change field "issue"" – schneida Oct 08 '16 at 14:18
  • Note than in this and many of the answers here, two-factor auth will prevent these from working. Create a personal access token in your github settings and use it instead of your password ... and voila! :) – berto Oct 21 '16 at 13:23
  • 1
    This has been deprected – Kirk Strobeck Apr 24 '17 at 20:48
144

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
wintersolutions
  • 5,173
  • 5
  • 30
  • 51
technoweenie
  • 846
  • 1
  • 8
  • 4
  • 1
    I copied in some of the sample code from that link. Hope you don't mind, and please let me know if I mistranslated it! – Tyler Dec 25 '10 at 07:14
  • 3
    You also need authentication, add this to command above: -u "login:password" – morgoth Feb 23 '11 at 14:00
  • 2
    I'd just like to add that this method still works, but it may have the side effect of listing your commit twice on the discussion page, if GitHub had already picked it up implicitly from the issue # in its message ([example](https://github.com/ask/celery/pull/450)). The commit only comes through once on the official pull request, though. – Greg Haskins Aug 27 '11 at 22:13
  • 3
    Can this be updated to the v3 API? GitHub just turned off the v2 API. – Michael Best Jun 13 '12 at 20:38
  • I've also attempted to update this to the v3 API... see if my edit goes through. – Tom Aug 06 '12 at 14:47
  • Here’s [an addendum to this answer](http://stackoverflow.com/a/11975092/578288). It demonstrates a shorter command using [HTTPie](https://github.com/jkbr/httpie), explains the `smparkes` example in a different way, and includes a password safety tip. – Rory O'Kane Aug 15 '12 at 18:38
  • +1... Just out of curiosity... Where do you write the name of your repository? I see the username and the branch name and from the issue you can get the issues's repository name, but I don't see your repository name. What's happen if you have two repositories with the branch synchrony? – xanatos Oct 13 '12 at 14:35
  • @xanatos It's determined by the request URL (https://api.github.com/repos/technoweenie/faraday/pulls) – Matt Enright Nov 21 '12 at 21:52
  • Is there any way of using this method but have curl (or similar) to ask for your GitHub password rather than typing it on as a command argument in clear text (with all the security concerns that entails)? – Ricardo Sanchez-Saez Apr 30 '13 at 10:14
  • 1
    @rsanchezsaez As I say in [my answer](http://stackoverflow.com/a/11975092/578288), change `--user "smparkes:password"` to `--user "smparkes"` to be prompted for your password interactively. – Rory O'Kane Jul 05 '13 at 07:05
  • Is there a `wget` equivalent? Would it be: `wget --user "smparkes" --post-data 'issue=15&head=smparkes:synchrony&base=master' https://api.github.com/repos/technoweenie/faraday/pulls`? I'm just not sure if the colon in the post-data needs percent-encoding (as `%3A`). – Craig McQueen Feb 19 '14 at 23:57
  • I had to remove the quotation marks around the issue id. Github seems to require integers now: ```message": "Invalid request. For 'properties/issue', "15" is not an integer.``` so the example becomes: ```curl --user "smparkes" \ --request POST \ --data '{"issue": 15, "head": "smparkes:synchrony", "base": "master"}' \ https://api.github.com/repos/technoweenie/faraday/pulls``` – wintersolutions Sep 13 '19 at 05:19
17

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:

nulltoken
  • 64,429
  • 20
  • 138
  • 130
10

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 smparkes@api.github.com: hunter2

Now Issue #15 is a Pull Request.

Community
  • 1
  • 1
Rory O'Kane
  • 29,210
  • 11
  • 96
  • 131
5

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.


Ismoh refers in the comments to the discussion:

"GitHub Action: Pull request 'development' object/data, aka linked issues"

And Ismosh's Marketplace action for finding the linked issues of a pull request.

    - name: Find linked issues
      id: find-linked-issues
      uses: Ismoh-Games/find-linked-issues@v0.0.1
      with:
        token: ${{ secrets.GITHUB_TOKEN }}
        repository: ${{ github.repository }} # optional
        pull-request-number: ${{ github.event.pull_request.number }} # optional
        copy-issues-labels: true # optional
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Search API sounds promising. I still don't understand why it isn't in REST API pull requests, but nice to know! – Ismoh Jan 04 '23 at 19:45
  • 1
    @Ismoh You might try the [search syntax for linked issues and pull requests](https://docs.github.com/en/search-github/searching-on-github/searching-issues-and-pull-requests#search-for-linked-issues-and-pull-requests) with the official GitHub CLI [`gh pr list --search `](https://cli.github.com/manual/gh_pr_list). – VonC Jan 04 '23 at 20:40
3

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
Jörn Hees
  • 3,338
  • 22
  • 44
  • 1
    Yes, 2FA prevents many of the answers here from working. In my case, I created a personal access token and use it rather than my password, which works. – berto Oct 21 '16 at 13:22
1

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.

Omar Ali
  • 8,467
  • 4
  • 33
  • 58
0

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.

Gautam
  • 1,079
  • 14
  • 21
0

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.

Kyle Gibson
  • 1,150
  • 1
  • 9
  • 12