If you’re like most people, you probably use GitHub to store your code and collaborate on projects with friends and colleagues. But did you know that you can also use the command line to manage your repositories? In this article, we’ll show you how to create a new repository, add files, and push changes to it. First, let’s create a new repository on GitHub. To do this, open a terminal window and type the following command: git init This will create a new repository in your account. You’ll need to provide a name for the repository (e.g., my-new-repo), and then hit enter. Next, we’ll add some files to our new repository. To do this, type the following command: git add . This will add all of the files in your current directory (i.e., everything inside of the current folder) to the repository. Finally, we’ll commit our changes by typing the following command: git commit -am ‘added some files’ ..
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.