[git] Can you issue pull requests from the command line on GitHub?

It seems like you have to interact with github.com to initiate a pull request. Is this so?

This question is related to git github pull-request github-cli

The answer is


I personally like to view the diff in GitHub prior to opening the PR. Additionally, I prefer writing the PR description on GitHub.

For those reasons, I made an alias (or technically a function without arguments), that opens the diff in GitHub between your current branch and master. If you add this to your .zshrc or .bashrc, you will be able to simply type open-pr and see your changes in GitHub. FYI, you will need to have your changes pushed.

function open-pr() {
  # Get the root of the github project, based on where you are configured to push to.  Ex: https://github.com/tensorflow/tensorflow
  base_uri=$(git remote -v | grep push | tr '\t' ' ' | cut -d ' ' -f 2 | rev | cut -d '.' -f 2- | rev)

  # Get your current branch name
  branch=$(git branch --show-current)

  # Create PR url and open in the default web browser
  url="${base_uri}/compare/${branch}/?expand=1"
  open $url
}

In addition of github/hub, which acts as a proxy to Git, you now (February 2020) have cli/cli:

See "Supercharge your command line experience: GitHub CLI is now in beta"

Create a pull request

Create a branch, make several commits to fix the bug described in the issue, and use gh to create a pull request to share your contribution.

cli/cli pr creation  -- https://i1.wp.com/user-images.githubusercontent.com/10404068/74261506-35df4080-4cb0-11ea-9285-c41583009e6c.png?ssl=1

By using GitHub CLI to create pull requests, it also automatically creates a fork when you don’t already have one, and it pushes your branch and creates your pull request to get your change merged.


And in April 2020: "GitHub CLI now supports autofilling pull requests and custom configuration"

GitHub CLI 0.7 is out with several of the most highly requested enhancements from the feedback our beta users have provided.
Since the last minor release, 0.6, there are three main features:

  • Configure gh to use your preferred editor with gh config set editor [editor].
  • Configure gh to default to SSH with gh config set git_protocol ssh.
    The default Git protocol is HTTPS.
  • Autofill the title and body of a pull request from your commits with gh pr create --fill.

So:

gh pr create --fill

Git now ships with a subcommand 'git request-pull' [-p] <start> <url> [<end>]

You can see the docs here

You may find this useful but it is not exactly the same as GitHub's feature.


I've created a tool recently that does exactly what you want:

https://github.com/jd/git-pull-request

It automates everything in a single command, forking the repo, pushing the PR etc. It also supports updating the PR if you need to edit/fix it!


I'm using simple alias to create pull request,

alias pr='open -n -a "Google Chrome" --args "https://github.com/user/repo/compare/pre-master...nawarkhede:$(git_current_branch)\?expand\=1"'

With the Hub command-line wrapper you can link it to git and then you can do git pull-request

From the man page of hub:

   git pull-request [-f] [TITLE|-i ISSUE|ISSUE-URL] [-b BASE] [-h HEAD]
          Opens a pull request on GitHub for the project that the "origin" remote points to. The default head of the pull request is the current branch. Both base and head of the pull request can be explicitly given in one  of  the  following  formats:  "branch",  "owner:branch",
          "owner/repo:branch". This command will abort operation if it detects that the current topic branch has local commits that are not yet pushed to its upstream branch on the remote. To skip this check, use -f.

          If TITLE is omitted, a text editor will open in which title and body of the pull request can be entered in the same manner as git commit message.

          If instead of normal TITLE an issue number is given with -i, the pull request will be attached to an existing GitHub issue. Alternatively, instead of title you can paste a full URL to an issue on GitHub.

A man search like...

man git | grep pull | grep request

gives

git request-pull <start> <url> [<end>]

But, despite the name, it's not what you want. According to the docs:

Generate a request asking your upstream project to pull changes into their tree. The request, printed to the standard output, begins with the branch description, summarizes the changes and indicates from where they can be pulled.

@HolgerJust mentioned the github gem that does what you want:

sudo gem install gh 
gh pull-request [user] [branch]

Others have mentioned the official hub package by github:

sudo apt-get install hub

or

brew install hub 

then

hub pull-request [-focp] [-b <BASE>] [-h <HEAD>]

I ended up making my own, I find that it works better the other solutions that were around.

https://npmjs.org/package/pullr


I've used this tool before- although it seems like there needs to be an issue open first, it is super useful and really streamlines workflow if you use github issue tracking. git open-pull and then a pull request is submitted from whatever branch you are on or select. https://github.com/jehiah/git-open-pull

EDIT: Looks like you can create issues on the fly, so this tool is a good solution.


Examples related to git

Does the target directory for a git clone have to match the repo name? Git fatal: protocol 'https' is not supported Git is not working after macOS Update (xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools) git clone: Authentication failed for <URL> destination path already exists and is not an empty directory SSL_connect: SSL_ERROR_SYSCALL in connection to github.com:443 GitLab remote: HTTP Basic: Access denied and fatal Authentication How can I switch to another branch in git? VS 2017 Git Local Commit DB.lock error on every commit How to remove an unpushed outgoing commit in Visual Studio?

Examples related to github

Does the target directory for a git clone have to match the repo name? Issue in installing php7.2-mcrypt How can I switch to another branch in git? How to draw checkbox or tick mark in GitHub Markdown table? How to add a new project to Github using VS Code git clone error: RPC failed; curl 56 OpenSSL SSL_read: SSL_ERROR_SYSCALL, errno 10054 How to add empty spaces into MD markdown readme on GitHub? key_load_public: invalid format git - remote add origin vs remote set-url origin Cloning specific branch

Examples related to pull-request

How to remove commits from a pull request How can I check out a GitHub pull request with git? Delete a closed pull request from GitHub How to do a GitHub pull request How do I update a GitHub forked repository? Undo a merge by pull request? How to cancel a pull request on github? Can you issue pull requests from the command line on GitHub?

Examples related to github-cli

Can you issue pull requests from the command line on GitHub? Is it possible to create a remote repo on GitHub from the CLI without opening browser?