Skip to content

Instantly share code, notes, and snippets.

@debloper
Created February 14, 2020 17:13
Show Gist options
  • Save debloper/bf0befeda9f7bd4c118d1afa19ba43d5 to your computer and use it in GitHub Desktop.
Save debloper/bf0befeda9f7bd4c118d1afa19ba43d5 to your computer and use it in GitHub Desktop.
Create commits and send pull requests on GitHub from CLI with cURL

Prerequisites

  • Personal Access <token> for <user> with all repo[...] access (may need more permissions, depending on the commit)
  • The <user> having write/push access to the existing <branch> of the intended <repo> of <org|user>
  • For creating new commits
    • The base64 encoded file content <base64content> is required.
    • To update an existing file, the <sha> hash of the file that's being replaced on GitHub repo is required

Creating Commits

Creating commits with /contents endpoint come with a limitation (as of APIv3) that it can only create/update a file. Updating multiple files (with a DIFF) doesn't seem to be possible very easily. The SHA sum is only required to update a single file with new contents, not for creating new file.

curl -X PUT -u <user>:<token> -d '{"message": "Commit Message", "branch": "<branch>", "content": "<base64content>", "sha": "<sha>"}' https://api.github.com/repos/<org|user>/<repo>/contents/<path>/<to>/<file.ext>

Creating Pull Requests

Creating pull request is relatively easier than creating commits. The "base" branch is generally the upstream master, and "head" is the downstream (as username:feature-branch) or working branch on the same repo (then only feature-branch) but both are pushed on GitHub, not are on local system. The "body" of the PR is optional.

curl -X POST -u <user>:<token> -d '{"title": "PR Title", "base": "master", "head": "feature-branch", "body": "Hello world!"}' https://api.github.com/repos/<org|user>/<repo>/pulls
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment