Getting Started With the Github CLI
Tags: github, githubcli, cli, commandline
Published: 2020-11-08 10:38:52



> ###_[GitHub](https://github.com), the popular [Git](https://git-scm.com) Provider has created a command-line tool that allows its users to access `GitHub` without leaving their terminals!_

---

In this post, I'll walk you guys through the process of setting up the [GitHub CLI](https://cli.github.com) on your local machine.

So, let's start!


## Step 1 - Install the GitHub CLI

Before you can start using the GitHub CLI, you must install it on your local machine.

> ### Installing on Mac

If you're using `macOS,` you can install it using [Homebrew](https://formulae.brew.sh/formula/gh#default), like so:

```powershell
brew install gh
``` 

Here's a gif to give you a better idea:

![Installing the GitHub CLI. Image Credit: Pratik Chaudhari](https://dev-to-uploads.s3.amazonaws.com/i/irpik5mzby19pueas4rt.gif)

<figcaption>Installing the GitHub CLI. Image Credit: Pratik Chaudhari</figcaption>

> ### Installing on Linux

For `Linux` users, too, [Homebrew](https://formulae.brew.sh/formula/gh#default) should work just fine.

Although, if you're using a [Linux distro](https://en.wikipedia.org/wiki/Linux_distribution) that isn't officially supported by `GitHub,` you'll have to rely on the community-maintained versions of the CLI.

Instructions for installing the CLI on such distros can be found [here](https://github.com/cli/cli/blob/trunk/docs/install_linux.md).

> ### Installing on Windows

If you're a `Windows` user, you can use [Chocolatey](https://chocolatey.org) to install the CLI, like so:

```powershell
choco install gh
```

Once installed, you can access it using the `gh` command, like so:

```powershell
gh
```

Here's what the above command will output:

![Output after firing the 'gh' command. Image Credit: Pratik Chaudhari.](https://dev-to-uploads.s3.amazonaws.com/i/ul5arjauc1tky02nbb19.gif)

<figcaption>Output after firing the 'gh' command. Image Credit: Pratik Chaudhari.</figcaption>

Now that the CLI has been installed and is accessible via the `Terminal`, let's do something interesting using the CLI, like viewing a [GitHub repository](https://guides.github.com/activities/hello-world/):

```powershell
gh repo view pratikgchaudhari/vue-3-projects
```

The above command is supposed to display all the info of the `vue-3-projects` repo created by its owner `pratikgchaudhari`, i.e., me.

But here's what we get instead: 

![Output of 'gh repo view pratikgchaudhari/vue-3-projects' command. Image Credit: Pratik Chaudhari.](https://dev-to-uploads.s3.amazonaws.com/i/w2tu8zdbditfpoupv0ze.gif)

<figcaption>Output of 'gh repo view pratikgchaudhari/vue-3-projects' command. Image Credit: Pratik Chaudhari.</figcaption>

The `GitHub CLI` requires you to authenticate yourself with your GitHub credentials before doing anything else with it!

And that brings us to our next step! 

## Step 2 - Authentication

To authenticate yourself, punch in the following command:

```powershell
gh auth login
```

Here's what you'll see:

![Output of 'gh auth login' command. Image Credit: Pratik Chaudhari.](https://dev-to-uploads.s3.amazonaws.com/i/ndzjbqbpzlr0qnnr3lsc.png)

<figcaption>Output of 'gh auth login' command. Image Credit: Pratik Chaudhari.</figcaption>

The `GitHub CLI` is asking you whether you want to sign in using your account on [GitHub.com](https://github.com), which is free for all users, or whether you want to use the [GitHub Enterprise](https://github.com/enterprise) account (most probably owned by your employer or maybe just you).

Since I don't have an enterprise account, I'll proceed with my `GitHub` account. 

Here's what I see after selecting the said option:

![Output after selecting the 'GitHub.com' option. Image Credit: Pratik Chaudhari.](https://dev-to-uploads.s3.amazonaws.com/i/kb1l4u3cwno5iit5emg6.png)

<figcaption>Output after selecting the 'GitHub.com' option. Image Credit: Pratik Chaudhari.</figcaption>

Since I don't have an `authentication token` (yet), I'll choose the `Login with a web browser` option.

Once I choose the said option, the CLI provides me with a `one-time` code:

![Output after selecting the 'Log in with a web browser' option. Image Credit: Pratik Chaudhari.](https://dev-to-uploads.s3.amazonaws.com/i/tgzf7k3r37zd4fgsxm9h.png)

<figcaption>Output after selecting the 'Log in with a web browser' option. Image Credit: Pratik Chaudhari.</figcaption>

I copy the `one-time` code provided by the CLI and press the `Enter/Return` key.

As expected, a browser window opens where I need to paste the `one-time` code that I just copied:

![The browser window where the 'one-time' code must be pasted. Image Credit: Pratik Chaudhari.](https://dev-to-uploads.s3.amazonaws.com/i/hdt87heocx5qjg7h74ye.png)

<figcaption>The browser window where the 'one-time' code must be pasted. Image Credit: Pratik Chaudhari.</figcaption>

After I click the `Continue` button, I see the following screen:

![The OAuth authorization interface for 'GitHub CLI' in browser. Image Credit: Pratik Chaudhari.](https://dev-to-uploads.s3.amazonaws.com/i/botbl321deg03r8v9bzq.png)

<figcaption>The OAuth authorization interface for 'GitHub CLI' in browser. Image Credit: Pratik Chaudhari.</figcaption>

Basically, I'm being prompted to authorize the `GitHub CLI` to access various resources from my GitHub account.

So, I'll grant it the access that it requires by clicking on the `Authorize github` button. Here's what I see next:

![The OAuth authorization success interface in browser. Image Credit: Pratik Chaudhari.](https://dev-to-uploads.s3.amazonaws.com/i/r6e22b975gesswvxm242.png)

<figcaption>The OAuth authorization success interface in browser. Image Credit: Pratik Chaudhari.</figcaption>

The above screen tells me that the authentication process is complete!

When I switch to my `Terminal` and press the `Enter/Return` key, the CLI asks me to choose between the following two protocols that it'll use while communicating with the `GitHub` server:

1. HTTPS
2. SSH

![The 'Terminal' prompting user to select a communication protocol. Image Credit: Pratik Chaudhari.](https://dev-to-uploads.s3.amazonaws.com/i/6tczlktiqkjvzq70rn59.png)

<figcaption>The 'Terminal' prompting user to select a communication protocol. Image Credit: Pratik Chaudhari.</figcaption>

I'll choose the `SSH` protocol here, but there's no harm in selecting the `HTTPS` protocol either:

![Output after selecting the SSH protocol. Image Credit: Pratik Chaudhari.](https://dev-to-uploads.s3.amazonaws.com/i/zwvcay4a2dnnxlkxxq9i.png)

<figcaption>Output after selecting the SSH protocol. Image Credit: Pratik Chaudhari.</figcaption>

And it's done!

Let's execute the following command again and see what happens:

```powershell
gh repo view pratikgchaudhari/vue-3-projects
```

![Output of 'gh repo view pratikgchaudhari/vue-3-projects' command. Image Credit: Pratik Chaudhari.](https://dev-to-uploads.s3.amazonaws.com/i/5qjqr99hhp1esta5bq5z.png)

<figcaption>Output of 'gh repo view pratikgchaudhari/vue-3-projects' command. Image Credit: Pratik Chaudhari.</figcaption>

As you can see in the above screenshot, the title of the repository along with the contents of its `README` are displayed on the `Terminal`.

The `GitHub CLI` provides many more commands which I'll be covering in detail in a separate article in the future.

For now, this is it!