104

How to submit a pull request from an existing locally-cloned repo?

Often, I want to look at some libraries source code from github, so I clone it. Later, I discover some issue with the code and raise it on a mailing list, often in passing. The library author says "nice find, can you send a pull request?".

And the answer is "not that easily". I haven't forked the repo yet, Ive cloned it. And there doesn't seem a way I can find to submit a pull request from a cloned repo?

If this limit is true, it feels like the sensible reaction is to fork anything and everything you ever look at, just so that if you ever might want to contribute, you can. And that fills up your github account with many inactive forks.

Doesn't seem a lot of talk about this issue - am I the only person whom this problem affects?

Ben Hutchison
  • 2,433
  • 2
  • 21
  • 25
  • 4
    Since noticed this very similar question: http://stackoverflow.com/questions/4209208/how-to-convert-a-readonly-git-clone-from-github-to-a-forked-one – Ben Hutchison Feb 27 '14 at 23:30

3 Answers3

75

Fork the repo on GitHub, then add your fork repo as a remote to your local cloned copy:

git remote add myfork https://github.com/<myGitHubAccountName>/<repoName>.git

Then you can push to your fork:

git push myfork master

If you're doing more than just this one pull request, you can remove the origin remote and name your fork as origin:

git remote rm origin
git remote add origin https://github.com/<myGitHubAccountName>/<repoName>.git

This is typically what I do. Sometimes I add the original origin as upstream so I still have a reference to it.

Gab是好人
  • 1,976
  • 1
  • 25
  • 39
bobthecow
  • 5,047
  • 25
  • 27
  • 6
    How do you do this without forking? – Russia Must Remove Putin May 03 '15 at 19:42
  • 3
    If you have push access to a repo, you can open a pull request between branches of that repo. You'll need to push your local branch to a new branch name on `origin` — something like `git push origin HEAD:my-feature`. Then you can use the web UI to open a pull request from `my-feature` to `master`. – bobthecow May 05 '15 at 09:30
  • 14
    @Torek - Yet another screwed up workflow. Is it *really* this difficult to perform a simple action? I thought this was one of the primary goals of git - make collaboration and sharing of patches easy in a decentralized model. It does not take a rocket scientist to deduce the goal should be a simple and easy to achieve since its a fundamental workflow. Those guys need to hire a UX expert since they all lack the common sense to figure this out on their own. – jww Nov 11 '16 at 05:41
11

If you're ok with installing another binary in your path, github has released a nice little tool called hub.

If you've cloned someone else's repo:

$ hub fork  # This creates a fork and adds your repo as a remote

$ git push YOUR_USER feature  # push the changes to your new remote

$ hub pull-request  # will open your browser
rdrey
  • 9,379
  • 4
  • 40
  • 52
  • Thanks for sharing about Hub and I love it :) I was in exactly this situation and hub make it so convenient for me. – Wahib Ul Haq Apr 25 '19 at 15:35
  • It's important to note that "hub fork" still creates the forked repo out on your account on GitHub. – Granger Apr 28 '20 at 17:39
0

I always clone instead of fork as well and the following steps work for me:

  1. Create a new branch on your cloned repo and make the new change.
  2. Push the change to your branch as the following:

    git push origin insert_your_working_branch_name

  3. Now you should be able to find your working branch in pull request from github master.

ohmyan
  • 337
  • 2
  • 10
  • 4
    I don't understand how step 2 above could work, if you've cloned someone's repo you don't have push rights to? – Ben Hutchison Oct 16 '19 at 03:33
  • this worked for me. for step 1, use git branch yourbranchname, git checkout yourbranchname then you can use the line in step 2. – ssy Mar 06 '21 at 13:50