Ciro Santilli

GitHub

The git URL is then git@github.com:userid/reponame.git

Pull request refs

GitHub stores refs to pull requests on the original repository under refs/pull/<number>/head.

Therefore, if you want to get a pull request locally to try it out on a local branch you can do:

git fetch origin pull/<pr-number>/head
git checkout -b <local-branch-name> FETCH_HEAD

GitHub also offers the merge request directly at refs/pull/<pull-request-id>/merge.

Create branches for all pull requests on git fetch:

git config --local --add remote.origin.fetch '+refs/pull/*/head:refs/remotes/origin/pr/*'

GitHub API v3 via cURL

GitHub has an HTTP REST API, which allows you to:

curl is a convenient way to use the API manually.

Vars:

USER=user
REPO=repo
PASS=

GET is the default request type:

curl https://api.github.com/users/$USER/starred?per_page=9999909 | grep -B1 "description" | less

Make a POST request with curl:

echo '{
  "text": "Hello world github/linguist#1 **cool**, and #1!",
  "mode": "gfm",
  "context": "github/gollum"
}' | curl --data @- https://api.github.com/markdown

GitHub API Authentication

Many methods that take a user can use the authenticated user instead if present.

Basic with user password pair:

curl -u 'cirosantilli' https://api.github.com/user/orgs

Or:

curl -u 'cirosantilli:password' https://api.github.com/user/orgs

GitHub API OAuth

OAuth: generate a large random number called the access token. Which you can only get once.

There are two ways to get the token:

Tokens are safer than storing the password directly because:

Once you get the token, make an authenticated request with:

curl https://api.github.com/user?access_token=$TOKEN

or:

curl -H "Authorization: token $TOKEN" https://api.github.com

Rate limiting

http://developer.github.com/v3/#rate-limiting

per_page

Get given number of results. Default is 30. Allows you to beat web API limitations. List all starred repos of a user:

curl https://api.github.com/users/$USER/starred?per_page=9999909 | grep -B1 "description" | less

Get repo info

Lots of info:

curl -i https://api.github.com/users/$USER/repos

Create repo

USER=
REPO=
curl -u "$USER" https://api.github.com/user/repos -d '{"name":"'$REPO'"}'

Repo name is the very minimal you must set, but you could also set other params such as:

curl -u "$USER" https://api.github.com/user/repos -d '{
   "name": "'"$REPO"'",
   "description": "This is your first repo",
   "homepage": "https://github.com",
   "private": false,
   "has_issues": true,
   "has_wiki": true,
   "has_downloads": true
}'

Its just JSON (remember, last item cannot end in a comma).

Delete repo

curl -u "$USER" -X DELETE https://api.github.com/repos/$USER/$REPO

Careful, it works!

hub

Powerful CLI interface: https://github.com/github/hub

gem install hub

Open URL of current branch / commit in browser:

hub browse

Create repository with same name as current dir:

hub create

Give a name and a description:

hub create name -d 'Description'