Roman's avatar

Hi, my name is Roman. I run a job board and make tutorials for BSD users. I'm fan of OpenBSD, POSIX-shell, and vi.

Twitter - RSS - Patreon - PayPal

Use GitHub from your command line

Why use the command line? Okay, GitHub has a clean and straightforward website, but when I'm in Terminal all day long, it's much easier to write a function and put it into my .bashrc than switch to a web browser and click buttons.

Basic setup

First, make sure you have enabled two-factor authentication for your GitHub account.

Then generate SSH keys and add your public key to your profile on GitHub so you can connect to GitHub via SSH. Test it right away.

$ ssh -T

By the way, did you know GitHub makes your public key available via HTTPS? Having your keys accessible is convenient, for example, when you're on one of your remote servers.

$ curl >> ~/.ssh/authorized_keys


Explore the documentation.

To parse the JSON from API responses, I suggest to install jq.

Let's play with the /user/keys endpoint to see what's possible with the GitHub API. For this endpoint you need your personal access token. Get one on your profile page or via command line. To make the following examples work your token should be in the admin:public_key scope.

Get access token

Replace USERNAME, OTP_CODE, and NOTE with actual values:

$ curl -s \
-H 'X-GitHub-OTP: OTP_CODE' \
-d '{"scopes": ["admin:public_key"], "note": "NOTE"}' |
jq -r .token

In response you will receive your personal access token. Treat it as carefully as a password.

List your public keys

You can now check your public keys via API. Replace ACCESS_TOKEN with your personal access token:

$ curl -s \
-H 'Authorization: token ACCESS_TOKEN' |

Add your public key

You can even add new public keys. Replace ACCESS_TOKEN, TITLE, PUBLIC_KEY with actual values, where PUBLIC_KEY is a path to your public SSH key:

$ curl -s \
-H 'Authorization: token ACCESS_TOKEN' \
-d "{\"title\": \"TITLE\", \"key\": \"$(cat PUBLIC_KEY)\"}" |

Delete your public key

To delete a key you need to get its ID at GitHub. Do not forget to replace ACCESS_TOKEN and ID with actual values.

$ curl -X 'DELETE' \
-H 'Authorization: token ACCESS_TOKEN'

Happy curling.

Tested on macOS 10.13