Create a New Github Repo Oh, So Easily with the new Github CLI

Peter Faso
3 min readSep 22, 2020

At least for me, a minor irritation when starting a new project in which I will be using version control is the need to create both a local repo and then separately create the remote. I know my workflow may not be the best or most ideal, however it is how I work and I am sure similar to how many others do as well. I would typically begin scaffolding my project — getting NPM packages, PIP libraries, or whatever else I may need depending on the language and type of project — and then laying out some basic initial code. Then Ill realize that its probably a good time to get an initial commit in. At that point I will initialize a local repo, add a .gitignore, and make my first commit. Good to go. Next, sometimes at that exact moment, or other times down-the-road, I may want to push my commits so I may work on another computer or head to the office. It’s at this point that I get slightly annoyed at having to break my workflow because I need to head to Github.com to create the remote repo and then connect it to my local. Well, this morning I found another article on the new Github CLI, and after one try all I can say is ‘Goodbye’ to at least one minor irritation.

Below I will explain just how quick and easy it is to create and connect your Github repo and connect it to your local, all within the terminal.

Install Github CLI

First thing you will need to do is install the new CLI, which is easy to do no matter what system you are using:

macOS

brew install gh

*You must have Homebrew installed

Windows

*You must have Chocolatey installed

choco install gh

Note: There are other installation methods, but I found these the easiest and ones that most users will probably use. For full installation please see the official Github README.

After that, just reset your terminal and you are good-to-go.

Prepare your project

Once you are in your working directory that your project will be housed in, create your local repo with your traditional git init.

Nothing new up to now. But here is where the new cli comes into play and things get interesting. Typically in the past, you would at this point need to leave your work, head over to the browser and bring up GitHub. From there you would create a new remote repository, usually matching the name of your local project, and then head back to the terminal and run a few lines to connect your local repo with the remote you just made in GitHub. Now, none of this is difficult. However, it is just a hassle. Let’s see how easy this is with the new CLI.

Create your remote repo quicker than ever

Still in your working directory that you just initialized your repo in, enter:

gh repo create

You will then see a few options come up for your remote settings:

? Repository name Java
? Repository description demo repo
? Visibility [Use arrows to move, type to filter]
> Public
Private
Internal

After completing this step you now have a remote repo on GitHub with the name that you chose. If you feel like it, head over to your GitHub and allow yourself a smile.

At this point your local repo and remote are not yet connected. To do this, first stage what you want to track with git add and then commit like you normally do. After that, set your local and remote branches with:

git push --set-upstream origin master

In one fell swoop you set your remote (origin) as the upstream for your master branch that you are working in.

Thats it! Once you complete one or two of these, you’ll be making local and remote repos in under a minute.

--

--