Simplify common operations with GitHub CLI

October 8th 2021 GitHub

Most of my blog posts have an accompanying GitHub repository with sample source code. On average, I create one such new repository per week, and they all share most of their configuration options. I looked at several options for automating repository creation to speed it up and reduce the likelihood of misconfigurations due to human error. The option I liked best was GitHub CLI and its alias command.

There are many different ways to install GitHub CLI on any supported platform. I chose Chocolatey to install it on my Windows machine (this requires administrator privileges):

choco install gh

Once GitHub CLI is installed, you will need to log in with your GitHub credentials:

gh auth login

GitHub CLI has a built-in wizard that will guide you through the login process. Most importantly, you can choose to log in with a browser or with an authentication token. If you choose the first option like I did, you'll need to copy an 8-digit one-time code from the command line and paste it into the GitHub web page that the tool opens for you. You may also need to re-enter your password into the browser during this step.

The wizard also gives you the option to choose between the HTTPS and SSH protocols for Git operations. This affects how GitHub CLI initializes remotes for your repositories. If you choose SSH, you will also be offered to upload your SSH key to GitHub if you have not already done so yourself.

Once GitHub CLI was fully configured, it was time to create the alias. I wanted the following configuration for each repository with blogpost sample code:

  • To be created in the DamirsCorner organization.
  • Be public.
  • Have an MIT license set up.
  • Have the wiki disabled.
  • Have a default description that includes the title of the blog post.
  • Include a link to the blog post.
  • Be tagged with selected topics.

First, I needed to figure out which GitHub command I could use to accomplish the above. It was going to be repo create with the right options. Here is an example for this blog post:

gh repo create DamirsCorner/20211008-github-cli-alias --homepage="" --description="Sample project for 'Simplify common operations with GitHub CLI' blogpost" --license=mit --public --enable-wiki=false --confirm

This command configures everything as described above, except for setting the topics. Unfortunately, setting the topics is not (yet?) supported by GitHub CLI, so I still have to do this manually once the repository is created.

In order to create an alias for this command, I had to determine which parts needed to be parameterized, as they are different for each repository. There are three such values:

  • Repository name, e.g. 20211008-github-cli-alias.
  • The page part of the blog post URL: 20211008-SimplifyCommonOperationsWithGitHubCli.html.
  • Title of the blog post, e.g. Simplify common operations with GitHub CLI

These values are replaced with positional placeholders in the expansion for the alias set command:

gh repo create DamirsCorner/$1 --homepage="$2" --description="Sample project for '$3' blogpost" --license=mit --public --enable-wiki=false --confirm

When you create the alias with the alias set command, the expansion string above must be properly quoted. For the command prompt, this means that all double quotes are escaped with a backslash:

gh alias set createBlogSample "repo create DamirsCorner/$1 --homepage=\"$2\" --description=\"Sample project for '$3' blogpost\" --license=mit --public --enable-wiki=false --confirm"

When using PowerShell, the $ characters in placeholders must be escaped with a backtick and all escaped double quotes must be doubled:

gh alias set createBlogSample "repo create DamirsCorner/`$1 --homepage=\""`$2\"" --description=\""Sample project for '`$3' blogpost\"" --license=mit --public --enable-wiki=false --confirm"

Once the alias is created, it can be invoked with positional parameters specified in the correct order. The following command passes the values from the original sample repo create command:

gh createBlogSample "20211008-github-cli-alias" "20211008-SimplifyCommonOperationsWithGitHubCli.html" "Simplify common operations with GitHub CLI"

If this command is invoked from a folder with an already initialized local Git repository, GitHub CLI also sets its origin remote to the newly created remote repository. Because there is already a commit in the remote repository (due to the LICENSE file), all local commits must be rebased when you first pull from the remote repository:

git pull --rebase

I mentioned earlier that after creating the repository, I still need to manually set its topics. To quickly open the repository in the web browser and do this, I use the browse command:

gh browse

GitHub CLI is a great tool that lets you perform most GitHub operations from the command line. You can use the alias command to create shortcuts for commonly used commands. In this blog post, I described how I created an alias for creating properly configured repositories with sample code from my blog posts.

Get notified when a new blog post is published (usually every Friday):

If you're looking for online one-on-one mentorship on a related topic, you can find me on Codementor.
If you need a team of experienced software engineers to help you with a project, contact us at Razum.
Creative Commons License