If you’re a software developer, then you know the power of the command line. With a few simple commands, you can create and manage your repositories on Github. Here’s how to use the command line to create and manage a Github repository:
- Open a new terminal window and navigate to your Github account.
- Type “git init” to initialize your repository.
- Type “git add” to add new files to your repository.
- Type “git commit” to commit your changes to your repository.
- Type “git push” to push your changes into the live branch of your repository on Github.
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:
This may prompt you to trust the key for Github’s package repository.
Once installed, you’ll need to 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:
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.