How to Use GitHub CLI — The New GitHub Command-Line Interface | Namu

AI Applied
The Startup
Published in
7 min readOct 8, 2020

GitHub CLI is GitHub on the command line. It brings pull requests, issues, and other GitHub concepts to the terminal next to where you are already working with git and your code. — GitHub

This tutorial is a short 10 minute read if you follow along, and will completely guide you on how to get started. We will look at how to install, authenticate, clone a repo, create a new repo, check status, add files, make new commits and finally, push your changes to GitHub. This tutorial will get you well on your way to using this new tool, please share it with any of your friends that are new to git in the command-line. Let’s get started.

I have written a French version of this same article here → https://medium.com/@namusanga/comment-utiliser-github-cli-la-nouvelle-interface-de-ligne-de-commande-github-namu-ad4f75a70b51

Using -main instead of -master

We will start on a simple note, due to the recent controversies with black movements and #BlackLivesMatter there has been a general push for developers to move on from calling the main branch in git -master to -main. Due to the relationship between the word ‘master’ in git and as it was used during slavery. We want to be as inclusive as possible and will adopt anything to make everyone feel as comfortable as we can and push the community forward, we ❤ you all. That said, github has recently changed their documents to show the main branch as -main and not -master, this is true by default for all new repos as well, please take careful note of this. Old documentations that still use master will return an error if pasted without changing master to main or vice-versa if you’re using an old repo.

The main branch will now be called main, period. Mom said so.

Installation

Yes, GitHub CLI does require git to be installed on your computer, therefore you will need to install git before continuing. It uses git internally for all functions that can also be done by git like, cloning, pushing, merging etc.

You can download and install git from this link → https://git-scm.com/downloads.

Next, download GitHub CLI from here → https://github.com/cli/cli/releases/ and click next on every single dialog that pops up during installation. Easy as that we can move to authenticating GitHub CLI.

Make sure to install the .msi for windows

Authentication

After installing GitHub CLI we need to login and get it setup with our GitHub account. To do this you will need to have a GitHub account, if you don’t, you can make one here → https://github.com/join it takes less than 5 minutes. Make sure you’re signed into your github account in your browser.

Then open Command Prompt from the start menu and run.

gh auth login

This will cause GitHub CLI to start asking you for details about your account.

Choose what type of github account you have

Usually you will have a GitHub.com account, but if you company has on-sight servers for storing GitHub project, then you need to choose GitHub Enterprise Server, press enter.

Next you need to login, select login with browser, this allows your browser to automatically authenticate you into your account, press enter.

Next select the one-time code given to you and paste it in the new tab that opens in your browser, and press enter.

And boom! just like that, github is asking you to verify you want this CLI to access your account, too easy! Click authorize and enter your password on the next screen.

Woohoo!!!!

If you head back to command prompt, gh, is asking you to choose a protocol, let’s go with HTTPS for simplicity.

Hahahaha — All done!

Enjoying this article so far? Come hang out with me on Twitter, I evaluate new technologies on a regular to see if they will be helpful and save time for your projects 😊.

Cloning Existing Repo

With this new CLI it’s even easier to clone a repo, we will use this one for this example → https://github.com/namusanga/How-To-Use-GitHub-CLI-Windows . Run:

gh repo clone https://github.com/namusanga/How-To-Use-GitHub-CLI-Windows

As you can see, gh gracefully cones the repo and creates its folder in my directory. Now run cd followed by the name of the repo to enter its folder.

cd How-To-Use-GitHub-CLI-Windows

Running dir tells the command line to list all files in the directory.

Just like that, we have cloned a remote repo, next we need to learn to create a local repo from scratch.

Creating A New Repo

Close the previous command line and open a new one, let’s create a new repo using gh. One of the coolest features of github CLI is that when you create a repo on your machine it automatically gets created on you github account as well. Run

gh repo create [name]

Replace [name] with any random name for your repo. You will need to answer some interactive questions about whether the repo is public or private and whether or not it should be cloned on your machine.

My results while creating a public repo

For the rest of this tutorial, you need to be in the repo you have just created. Open a terminal in that location using the following instructions.

Opening A Terminal In A Location

As a git user, you need to know how to open a terminal in the location of a git project, it’s super easy though.

  1. Open the location in your file explorer.
  2. Copy the path to the location from the top bar.
  3. Open a terminal and type cd [path] replace the [path] with the content on your clipboard by simply right clicking in anywhere in the terminal. For terminals right-click usually means copy/paste.

Once you’re done opening a git directory on your terminal, you’re ready to start working on it.

Checking Status

Checking the status of a repo in gh remains the same as for git since gh doesn’t offer an extra check status function. In this section we shall simply look at the check status function for completeness. (bet you didn’t know that was a word 😂😂😂)

Anyway, checking status allows one to know which changes have been made to a project, new files, modifications, etc you can do this to know if you have anything to commit.

→ Create a new file in your repo called ‘testing-status.txt’.

→ Then head to the command line and type git status

This is what I get

Notice how it says → Untracked files. This means these are the files that need to be added to git. To add them run git add * This add all new files in your repo. If you run status now, the file is green, this means it has been staged ‘aka it is ready to be committed’

Before we do that though, let’s make a change to it. Open the file and type anything, and I mean anything!

There we go

If you check git status again now, there are two things that need committing.

  1. This file was created and
  2. It has been edited.

Now that we know what the status of our project is, it’s time to commit.

Run git commit -a -m "just added a random file to test git status" this command will tell git to save these changes.

Now that were done learning some git basics, it’s time to push our changes to the cloud.

Again gh doesn’t provide a function for this, but it does simplify the process, so just run git push

And just like that, we’re done!

--

--