Quick Links

Creating new Github repositories is a core part of many people's workflow, especially when starting new projects. Automating this process is possible using Github's own CLI tool, which allows you to create and manage your account's repos from the command line.

Why Automate Github?

If you do a lot of work with Github, you're probably familiar with the process of creating a repository, and linking it to your local

        git
    

 installation with

        git init
    

 or

        git remote add
    

.

Doing this manually can get a little tedious though, especially when it is context switching that takes you out of your workflow, just to go click some buttons and fetch a link from Github's website. Of course, if you're fine with that, you should just use the standard

        git init
    

 or

        git remote add
    

 method, and set it up the normal way.

But, Github does have a command line tool that can be used to easily create repos with a single command. It's commonly used to automate other tasks, like working on pull requests or issues, but has a subcommand for working with repos that can create and manage repositories.

Using Github's CLI

First, you'll need to install the CLI and link it to your Github account. This is fairly straightforward though; you can find releases and install instructions on the Github repo for the tool.

It's available for most package managers, including Windows's

        winget
    

, and all the binaries and installers are available as releases. For example, if you're on Windows, you can install it natively using the MSI. But, if you're using Windows Subsystem For Linux (WSL), or just regular Linux, you can install it from

        apt
    

:

sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-key C99B11DEB97541F0
    

sudo apt-add-repository https://cli.github.com/packages

sudo apt update

sudo apt install gh

This may prompt you to trust the key for Github's package repository.

Once installed, you'll need to login.

gh auth login

This will prompt you for a few things, and finally ask you to log in with your browser through OAuth, or manually create and paste an authentication token from your account's security settings.

Logging in with OAuth is easy though, and just requires you to press the button. It may fail to open your default web browser though, so you may have to manually press the link, and copy the 8 digit key shown in the terminal.

Then, you can make a repo very easily, specifying --public or --private when using the command:

gh repo create test --private

And you should see the new repository in your account:

The Github CLI has a bunch of other subcommands for working with repos:

  • gh repo edit, which can set a lot of different config flags, such as the default branch, whether the issues/wiki/project pages are turned on, and your homepage and description.
  • gh repo fork, which works like git clone except forking the target repository and making a copy in your account.
  • gh repo list, which prints out a list of your repositories.
  • gh repo rename, changes the name and URL.