Forking a GitHub repo using from the command line with bash, cURL, and the GitHub API

Viewed 9931

I am having trouble with my bash script to fork a GitHub repo using cUrl.
The gitHub API doc for creating a fork.

I've tried many variations:

curl -u $my_user_name https://api.github.com/repos/forks -d "{\"owner\":\"$upstream_repo_username\",\"repo\":\"$upstream_repo_name\"}"

and
curl -u $my_user_name https://api.github.com/repos/'$upstream_repo_username'/'$upstream_repo_name'/forks

yield the following error: { "message": "Not Found", "documentation_url": "https://developer.github.com/v3" }

In Contrast, the following Creates a new empty github repo, as expected:
curl -u $my_user_name https://api.github.com/user/repos -d "{\"name\":\"$upstream_repo_name\"}"

Any ideas on how to create a fork of a repo from the command line?

I have a bash script that: - creates an empty repo on github with the name of the repo I'm going to clone, - clones a repo from another user locally, and - pushes my cloned repo into the empty repo I created in my github account - sets origin and upstream remotes appropriately

However, this method does not keep a connection within GitHub to the source (forked) repo. I particularly like the convenience of the forked link appearing below my own repo name ;-)

The goal is to do all my cloning (and forking) from the command line.

I do not want to open a browser, navigate to the repository I wish to fork, just to access that "Fork" button.. only return back to the command line to finish the process.

Alternatively, can I turn a cloned repo into a forked one from the command line? (ie some command line api command that will re-create those internal github links that forks possess?)

5 Answers

I use hub. I have also set git as alias for it so that I don't have to worry everytime what command comes under git and what under hub, so my code looks just like I an running git. Follow installations here.

$ git clone <github_repo>
$ cd <github_repo>
$ git fork

PS: For the above code to work, before executing it in your .bashrc or .bash_profile you need to put the following

alias git=hub

I used this command to fork on github enterprise:

curl -vX POST \
    https://git.redacted.com/api/v3/repos/$upstream_repo_username/$upstream_repo_name/forks?access_token=$api-token \
    -d @gh-fork.json \
    --header "Content-Type: application/json"

gh-fork.json:

{
    "organization": "org-to-fork-to",
    "description": "",
    "homepage": "https://git.redacted.com",
    "private": false
}
Related