Search code examples
githubgithub-apigithub-cli

How to create a github pull request from a forked repository back to the original repository using the github API?


In git/github I want to propose a change to a single file.

To do so, I

  • fork the repository
  • clone the repository locally
  • make the changes and the commit on a new feature branch
  • push the feature branch to my forked repo
  • create and merge a pull request to merge the changes from the feature branch to master

Now how can I programatically (via API) create a new pull request to merge the changes I have put in the forked repo to the original repo? How to do that?

The documentation only seems to handle the case when it is the same repository.


Solution

  • The documentation can be confusing. The following request will create a new pull request on FORKOWNER/THEREPO to merge FORKER:newbranch into FORKOWNER:main:

    curl -X POST -H "Accept: application/vnd.github.v3+json" \
      https://api.github.com/repos/FORKOWNER/THEREPO/pulls \
      -d '{"title":"XYZ", "body":"ABC", "head":"FORKER:newbranch", "base":"main"}'
    

    This can also be done with the gh api command of the GitHub CLI:

    gh api --method POST -H "Accept: application/vnd.github.v3+json" \
      repos/FORKOWNER/THEREPO/pulls \
      -f title='XYZ' -f body='ABC' -f head='FORKER:newbranch' -f base='main'
    
    

    Or directly with the gh pr create command of the GitHub CLI:

    gh pr create --repo FORKOWNER/THEREPO \
      --title "XYZ" --body "ABC" --head FORKER:newbranch --base main