skip to content

Using GH to generate a new repo

gh is a lovely CLI tool from GitHub that lets you manage your repos from Terminal, including downloading license and .gitignore files.

TL;DR Create a blank repo with a LICENSE file, a README and a VisualStudio .gitignore

❯ gh repo create YOUR_REPO \
 -d "ADD A DESCRIPTION" \
 --add-readme --disable-wiki --private \
 --gitignore VisualStudio -l MIT

gh is a great time saver

On its own it gh may not seem that useful. But I use gh pr create -d, gh pr ready, gh pr view --web hundreds of times a week, and not having to stop what I’m doing and switch to a browser is great. I knew you could create repos with it, but I have only just learned a couple of new tricks

Using GH to get a list of GitHub licenses

You can query GitHub’s GraphQL or rest endpoints without having to add auth tokens (you login via gh itself with gh auth login). The help screen has a lot of useful samples, for example

❯ gh api -h
  Makes an authenticated HTTP request to the GitHub API and prints the response.
  ...

  # post an issue comment
  $ gh api repos/{owner}/{repo}/issues/123/comments -f body='Hi from CLI'

  # list releases with GraphQL
  $ gh api graphql -F owner='{owner}' -F name='{repo}' -f query='
    ...

To get a list of licenses just use the licenses built-in endpoint. Helpfully, gh is shipped with jq - you can summon itby passing the -q option

❯ gh api licenses -q '.[].name'
GNU Affero General Public License v3.0
Apache License 2.0
BSD 2-Clause "Simplified" License
BSD 3-Clause "New" or "Revised" License
Boost Software License 1.0
Creative Commons Zero v1.0 Universal
Eclipse Public License 2.0
GNU General Public License v2.0
GNU General Public License v3.0
GNU Lesser General Public License v2.1
MIT License
Mozilla Public License 2.0
The Unlicense

Using GH to get .gitignore files

Github doesn’t have a similar .gitignore endpoint. But it has a special repo for .gitignore files, github/gitignore, and you can use the normal api command to get files from it

# get the list of available .gitignores
❯ gh api /gitignore/templates -q ".[]"
AL
Actionscript
Ada
Agda
Android
AppEngine
...

# get the one you want
❯ gh api /gitignore/templates/VisualStudio -q ".source"
## Ignore Visual Studio temporary files, build results, and
## files generated by popular Visual Studio add-ons.
##
## Get latest from https://github.com/github/gitignore/blob/main/VisualStudio.gitignore
...

Creating a repo with a gitignore and a License file

The git repo create NAME command comes with a few useful flags, and that include the license and gitignore as above. Sadly you can only add a single .gitignore, if your repo uses multiple languages, you’ll have to add the others later

❯ gh repo create YOUR_REPO \
 -d "ADD A DESCRIPTION" \
 --add-readme --disable-wiki --private \
 --gitignore VisualStudio -l MIT
 ✓ Created repository YOU/YOUR_REPO on GitHub

❯ gh api /gitignore/templates/Python -q ".source" >> .gitignore

# "Node" is the .gitignore for JS and TS too
❯ gh api /gitignore/templates/Node -q ".source" >> .gitignore

I don’t create that many repos, but if I did it wouldn’t be too hard to wrap the above sequence of commands in a bash function.