4

I need to create a git pull-request from the command line, without installing any wrappers or additional software. Is there any way to do so in git? I can't seem to find any official git documentation which supports this.

5 Answers5

5

Surprisingly (to a lot of people), pull requests are first-class citizens in the git world and not something invented by GitHub.

Use git request-pull to create an E-Mail text you could send over to someone who should fetch some revisions from a server.

This server could be something very basic like a read-only file share or whatever.

The topic is discussed in-depth in the Git Book: https://git-scm.com/book/ch5-2.html

An example: you forked a repo, introduced a patch off the origin/master branch and publish it on a file share called //myserver/myrepo.git. Then you'd type

git request-pull origin/master //myserver/myrepo.git

Then take the output of the command, paste it into an e-mail and you have your pull-request.

eckes
  • 56,506
  • 25
  • 151
  • 189
1

Edit:

It seems you want a GitHub pull request, not Git pull request. To see the difference, this answer has a good explanation:

https://stackoverflow.com/a/6235394/245966

There is no such a thing as "pull request" in Git itself. The notion of pull requests was introduced to Git ecosystem by the platforms using Git, such as GitHub or Atlassian Stash.

Since it's not a "native" Git concept, you don't have any Git built-ins to open a GitHub pull request from command line.

There's a https://github.com/github/hub tool that can help you automate common GitHub flows from command line.

Having said that, when it comes to opening GitHub pull request or Atlassian Stash pull request, I wrote some command line tools that you can put in PATH to do that job. They were written for my specific use case, feel free to modify them to your needs and use them.

For GitHub:

For Atlassian Stash:

For them to work, you have to have origin and upstream branches properly configured in your repos. They work in a crude way, by parsing the output of git remote commands to construct proper GitHub/Stash pull request URLs and then open them in the browser.

The shell scripts are also checking additional stuff, like making sure you open the pull request from proper branch etc.

For GitHub, also have a look at

which can guess the number of the next pull request and put it in the commit message before you push.

Community
  • 1
  • 1
jakub.g
  • 30,051
  • 7
  • 78
  • 118
  • http://stackoverflow.com/q/7273434/520162 shows that git **does** have the concept of a pull-request... – eckes Jul 27 '15 at 10:06
  • @eckes thanks for fixing my ignorance. Was `git request-pull` added recently or it's been there forever? Honestly I never used it in last 3 years. – jakub.g Jul 27 '15 at 10:22
  • have a look at this post: http://vmiklos.hu/blog/using-git-request-pull.html. It's dated back to 2007, so I'd not call it a new feature :-) – eckes Jul 27 '15 at 10:31
1

Bash Script

Take a look at the following article which will help you to create PR from the command line.

Using hub package

  • Install hub package on your machine. Follow this link.
  • Checkout to a new branch:
    git checkout -b your-branch-name
    
  • Commit your changes or create an empty commit:
    git commit --allow-empty -m "Your commit message"
    
  • Push the changes:
    git push --set-upstream origin your-branch-name
    
  • Run the following command to create Pull Request:
    hub pull-request
    
Zeeshan Ahmad
  • 1,667
  • 15
  • 22
0

May 2020: you can use the GitHub CLI "gh"

See "GitHub CLI allows you to close, reopen, and add metadata to issues and pull requests"

GitHub CLI 0.8 makes working with pull requests and issues from your terminal even simpler. This release includes two primary features:

  • You no longer need to open your issue or pull request in the browser immediately after creating it just to add metadata.
    Now you can add reviewers, labels, assignees, projects, and milestones (as applicable) when creating pull requests and issues.
  • Close and reopen pull requests and issues right from the CLI with gh pr close, gh pr reopen, gh issue close, and gh issue reopen.

See how to upgrade in our README!

VonC
  • 1,042,979
  • 435
  • 3,649
  • 4,283
  • Not available for enterprise version :| – John Samuel May 19 '20 at 07:53
  • @JohnSamuel Yes, [the `README` mentions](https://github.com/cli/cli/commit/700f86c5bce165a93aa3ecbbc850363ea8e530d8) "We are planning support for GitHub Enterprise Server after GitHub CLI is out of beta (likely toward the end of 2020)". In the context of the OP's question, github.com is enough. – VonC May 19 '20 at 07:56
  • Oh yeah. I read that too :). but is there an alternate way to create a pull request from command line? – John Samuel May 19 '20 at 07:57
  • @JohnSamuel There is a preliminary support in the work though: https://github.com/cli/cli/pull/823 – VonC May 19 '20 at 07:58
  • Good to see that coming into shape :) – John Samuel May 19 '20 at 07:59
  • @JohnSamuel It will depend on your GHES version: https://github.com/cli/cli/issues/273#issuecomment-610007084 – VonC May 19 '20 at 08:00
-1

UPD: It looks like it's true and you can't do it "out of the box". you'll have to install a wrapper. Check this Can you issue pull requests from the command line on GitHub? maybe you can find a solution there.

Community
  • 1
  • 1
Eugene Nezhuta
  • 1,457
  • 1
  • 7
  • 7
  • 1
    While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – greg-449 Jul 27 '15 at 09:03
  • @greg-449 you're right that will not solve your problem. I updated my answer – Eugene Nezhuta Jul 27 '15 at 09:14