Building a GitHub repository from the command line with bash, cURL and the GitHub API
I am having problems with my bash script to deploy a GitHub repository using cUrl.
The gitHub API doc for creates a fork .
I've tried many options:
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
output the following error:
{
"message": "Not Found",
"documentation_url": "https://developer.github.com/v3"
}
In contrast, the following Creates a new empty github repository 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 the repo from the command line?
I have a bash script that: - creates an empty repo on github with the name of the repo I am about to clone, - clones the repo from another user locally, and - pushes my cloned repo into the empty repo that I created in my github account - correct sets source and upstream remotes
However, this method does not support connecting inside GitHub to the original (forked) repo. I especially 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 don't want to open the browser, navigate to the repository that I want to use, only to access that Fork button. Go back to the command line to complete the process.
Alternatively, can I turn the cloned repo into a forked one from the command line? (i.e. some command line command line command that will recreate those internal github links that forks have?)
source to share
Here is my working bash script:
curl -u $my_user_name https://api.github.com/repos/$upstream_repo_username/$upstream_repo_name/forks -d ''
An example of using hardcoded strings instead of bash variables:
curl -u 'SherylHohman' https://api.github.com/repos/octocat/Hello-World/forks -d ''
Note. I moved -d ''
to the end to avoid login errors.
The request requires authentication.
I provide this via the curl parameter -u
(as opposed to using OAuth2).
When I used the parameter -u $my_user_name
,
I had to move -d ''
to URI - this resulted in login errors if they are between -u 'username'
and URI.
It turns out to be the main source of errors in my script with bash -syntax.
I had quotes surrounding bash variables that shouldn't have been.
(..just Solving the point of pain without knowing bash or curl)
Also, as #YuriSchimke pointed out, this particular URI required parameters must be passed in the URI. Passing these parameters as json is not an option, unlike the URI for creating a new empty repo.
This is why I was puzzled as to how to send this data in a URI:
Using curl, the default request is GET .
In curl, POST requests are created by adding a flag -d
(equivalently --data
) followed by the data being sent.
I needed to send a POST request.
The format for the GitHub API is that GET requests (and POST, like CreateRepo) can sometimes send some parameters as json strings or requests
NOTE. The documentation for the GitHub API looks a bit incomplete as I don't see any mention of an API allowing json, only the query string.
I suppose in this case the data is sandwiched between two static URI parts, making it impossible to send as json values.
I was at a loss how to use the -d
no data flag :
If I just left it, the API call was handled as GET.
It returned information about a repo I wanted to develop
instead of deploying the repo to my account.
@ YuriSchimke's post gave me this "Ahaa!" Thank you! I laugh that it didn't cross my mind. I am grateful that Yuri made it so obvious! (Thanks again).
source to share
The documentation shows that the owner and repo are part of the request URI
curl -d '' https://api.github.com/repos/octocat/Hello-World/forks
https://developer.github.com/v3/repos/forks/
This works fine.
source to share
It is much easier to create or unblock a repo using the hub command line tool.
Installation instructions: https://github.com/github/hub#installation
It can do a lot more, but here's how to deploy an online repo using the command line.
However, there is conflicting information, so it can be a bit confusing.
To unblock an online repo owned by someone else, follow these steps:
- clone the repo with
git clone
git clone ssh://git@github.com/keras-users/keras.git
- change to cloned repo
cd keras
- create a fork online
hub fork
To deploy your own repo that is already hosted on github:
Github does not allow you to unlock your own repo,
So you need to create a clone of your own repo on your machine
first.Then you can create a cloned repo on the github website.
- cloning your repo to your computer.
git clone ssh://git@github.com/alpha_beta_gamma/clone_repo.git
- clone a repo on your machine to another repo (also on your machine).
git clone clone_repo clone_repo2
- change directory to new cloned repo
cd clone_repo2
- create a cloned repo on Github
git create
This will create a new repo on GitHub.
- set the url of the remote repo:
git remote set-url https://github.com/username/clone_repo2
- push local repo to online repo
git push
source to share
I am using a hub . I also set it git
as an alias so that I don't have to worry about which command is in git
and which is in every time hub
, so my code looks like I'm running git. Follow the installs here .
$ git clone <github_repo>
$ cd <github_repo>
$ git fork
PS: For the above code to work, before executing it in yours .bashrc
or .bash_profile
you need to put the following
alias git=hub
source to share
Some of the old answers here don't work for me. These are the commands I ran to fork the repo that I technically already own.
# the initial clone of the repo we want to "fork"
git clone https://github.com/original
# clone from folder we just got from github to a new location (which will be created by this command)
git clone original my-fork
[create new repo for my-fork on githubcom]
# remote for the new repo
git remote set-url origin https://github.com/my-fork.git
# OPTIONAL. by setting this we can fetch and merge with the repo we "forked" from
git remote add upstream https://github.com/my-fork.git
# perform initial commit to new repo
git push -u origin master
source to share
I used this command to fork on github enterprise curl -vX POST https://git.redacted.com/api/v3/repos/<org-to-fork-from>/<repo-to-fork>/forks?access_token=<api-token> -d @gh-fork.json --header "Content-Type: application/json"
gh-fork.json is just
{
"organization": "org-to-fork-to",
"description": "",
"homepage": "https://git.redacted.com",
"private": false
}
source to share