Getting Started with GitHub Copilot in the CLI🚀
Tags: tutorial, cli, ai, productivity
Published: 2023-11-17 17:09:17

If you've ever used the terminal, you know the struggle of trying to remember a command, the fear of breaking your system with a random command you found on the interwebs or the frustration of trying to find the right command to do what you want. 

That struggle is lightened today, with [GitHub Copilot in the CLI](https://github.com/github/gh-copilot)! 💃🏼

<img src="https://media.giphy.com/media/JQFuCdFbQAbNaawknQ/giphy.gif" alt="black mance dancing and saying yay!" />

GitHub Copilot in the CLI provides a chat-like interface in the terminal that allows you to ask questions about the command line.

![Copilot CLI](https://user-images.githubusercontent.com/47188731/281628464-c25d4257-3c75-4804-beb6-bf99f3795848.png)

It is designed to help you with general shell commands, git commands and _gh_ cli commands. This post aims to get you up and running with installing, and using copilot in the cli.

## Installation

To install copilot in the cli, you must first install [GitHub CLI](https://cli.github.com/) and complete authentication in an OAuth browser window. If you're not familiar with the GitHub CLI, learn how to use it by [reading this blog post!](https://dev.to/github/top-10-tips-for-using-github-from-the-command-line-1me6)
Since I'm on macOS, I used homebrew as my package manager:

```bash
brew install gh
gh auth login
```

You also need to have an active [GitHub Copilot subscription](https://github.com/features/copilot) to use copilot in the cli. 
Once the `gh cli` is installed, and you have a copilot subscription, you can install the extension by running:

```bash
gh extension install github/gh-copilot
```

And you are ready to go! 🎉

## Available Commands

I recommend running the help command to understand how to interact with copilot in the cli. When you run `gh copilot --help` you will see the following output:


```bash
gh copilot

Your AI command line copilot.

Usage:
  copilot [command]

Available Commands:

  config      Configure options
  explain     Explain a command
  suggest     Suggest a command

Flags:
  -h, --help      help for copilot
  -v, --version   version for copilot

Use "copilot [command] --help" for more information about a command.

```

There are two primary commands you can use to interact with copilot in the cli: `gh copilot explain` and `gh copilot suggest`.

## Create an Alias 💡

> 💡 Hot tip! creating an alias will make using copilot in the cli a part of your regular workflow a lot easier.

After installation, the next thing I advise you to do is to create an alias. This will make interfacing with copilot in the cli a lot smoother because it saves you some keystrokes and you can get help faster. 

Since I'm using [zsh](https://github.com/ohmyzsh/ohmyzsh/wiki), I ran the following commands to create aliases:

```bash
alias copilot='gh copilot' ; echo 'alias copilot="gh copilot"' >> ~/.zshrc && source ~/.zshrc
alias gcs='gh copilot suggest' ; echo 'alias gcs="gh copilot suggest"' >> ~/.zshrc && source ~/.zshrc
alias gce='gh copilot explain' ; echo 'alias gce="gh copilot explain"' >> ~/.zshrc && source ~/.zshrc
```

Now, let's get into what these commands mean and a few examples on how to use them. 
> 💡 Remember, I have an alias so I'll be using `gce` or `gcs` instead of `gh copilot explain` or `gh copilot suggest`.

## Explain Command 🤖

There are two ways you can use the copilot cli explain command:

1. You can run the explain command with no arguments and it will prompt you to enter a command to explain.

```bash
gh copilot explain
```

![copilot explains prompt flow](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mu05y8xdnf2xnakooanr.gif)

2. You can run the explain command and the command you want explained at the same time:

```bash
gh copilot explain 'your query'
```

![copilot explains a command](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l71eof2w28tn36ahi112.gif)

You can also run the explain command with the `-h` flag to get more information:

```bash
gh copilot explain -h
```

![copilot explains a command](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/94s8l93riupprjlgt8e1.gif)

## Suggest Command 🤖

There are 3 ways you can interact with copilot in the cli to get a suggestion:

1. You can run the suggest command and follow the on screen prompts:

```bash
gh copilot suggest
```

![copilot suggests a command](https://github.com/LadyKerr/cli-skills-copilot-talk/assets/47188731/52376a70-9acc-41b3-9ed6-e5cd59c4bff1)

2. You can run the suggest command and your query at the same time, then select the type of command:

```bash
gh copilot suggest 'command you want to run'
```

![copilot suggests a command](https://github.com/LadyKerr/cli-skills-copilot-talk/assets/47188731/55da2faa-1afa-4f2c-b74f-72a41ab9e655)

3. You can run the suggest command, your query and the type of command at the same time:

```bash
gh copilot suggest 'command you want to run' -t 'type of command'
```

![copilot suggests a command](https://github.com/LadyKerr/cli-skills-copilot-talk/assets/47188731/54f28b58-6d69-4a75-aa71-aedf724e8648)

The `-t` flag means type of command and can be one of the following:
- git
- gh
- shell

## Revising Suggested Commands

Sometimes, the suggestion you get from copilot in the cli is not exactly what you want. You can revise the command by selecting the `Revise command` option when you receive a suggestion and give copilot about more detail about what you want:

![copilot suggests a command](https://github.com/LadyKerr/cli-skills-copilot-talk/assets/47188731/d09f890a-930c-4fff-82df-4ec41af3c31e)

> 💡 Hot tip! Copilot in the CLI is not there to simply give you the suggested you want. Remember, it is there as your co-pilot, so if the output is not quite what you expected, edit the suggestion or ask for a revision until you get what you need.

## Explaining Suggested Commands

When you get a suggestion from copilot in the cli, you can ask copilot to explain the command by selecting the `Explain command` option:

![copilot suggests a command](https://github.com/LadyKerr/cli-skills-copilot-talk/assets/47188731/9a63fcab-993b-47b0-8d78-5d2ca118a91f)

## Copying Suggested Commands

When you get a suggestion from copilot in the cli, you can copy the provided response and run it in your terminal.

## Rating Commands 🌟🌟🌟🌟🌟

When you get a suggestion from copilot in the cli, you can rate the command by selecting the `Rate command` option, and selecting whether or not it was helpful:

![copilot suggests a command](https://github.com/LadyKerr/cli-skills-copilot-talk/assets/47188731/4c14610c-94be-4c98-a799-3d97eab19d5c)

## Exiting the prompt flow window 🚪

You can exit the prompt flow window by selecting the `Exit` option:

![copilot suggests a command](https://github.com/LadyKerr/cli-skills-copilot-talk/assets/47188731/8ea4838b-9c56-44b7-8df2-b36b2ec53a7e)



# Examples of using Copilot in the CLI
Now let's take a look at some examples. 💃🏼

## 1. Explain a dangerous command ⚠️
So, I saw this command online and was curious about what it did so I can ask:

```bash
gh copilot explain chmod -r 777 /
```

![copilot explains a command](https://github.com/LadyKerr/cli-skills-copilot-talk/assets/47188731/59f3dd7f-2b6a-4fa3-8de7-43974e6b0eed)


> 💡 Hot tip! When asking copilot to explain commands that have special characters, enclose the command in a string so copilot can understand it better.

As you see, this command is actually very dangerous and can cause you to lose all your data. What I love about the explanation I received was that it not only told me what the command does, but it also told me why it was dangerous.

🚨 **NEVER RUN THIS COMMAND ON YOUR MACHINE** 🚨

## 2. Delete a git branch locally and remotely

In the visual example below, I first list the branches in my local repo with the command `git branch` then I list the branches in my remote repo with the command `git branch -r`. I then ask copilot in the cli for a suggestion:

```bash
gh copilot suggest 'delete a git branch locally and remotely' -t git
```

![Delete a git branch locally and remotely](https://github.com/LadyKerr/cli-skills-copilot-talk/assets/47188731/17fdc685-0a34-49fc-93e1-9269329c5c42)

I was provided with the following suggestion:

```bash
git push origin --delete <branch_name>

git branch -D <branch_name>
```

I ran the suggested commands, then listed the branches in my local repo and remote repo again to confirm that the branch was deleted. Thankfully, it was. 🙌🏼

## View all open issues in a repo

You can also ask copilot in the cli for `gh` commands. For example, if you wanted to view all open issues in a repo, you can ask copilot in the cli for a suggestion:

```bash
gh copilot suggest 'view all open issues' -t gh
```

I was provided with the following suggestion:

```bash
gh issue list --state='open'
```

I ran the command and was provided with a list of all open issues in the repo I was currently in. See the flow in the visual example below:

![View all open issues in a repo](https://github-production-user-asset-6210df.s3.amazonaws.com/47188731/283870990-3d8b3b01-bcd7-4d63-9cd9-5c88bf4c5b21.gif)

## 3. Kill processes with open files that have been deleted
Copilot in the CLI can also be useful for administrative tasks in the terminal. For example, if you wanted to kill processes with open files that have been deleted, you can ask copilot in the cli for a suggestion:

```bash
gh copilot suggest 'kill processes with open files that have been deleted' -t shell
```
I was provided with the following suggestion:

```bash
lsof | grep '(deleted)' | awk '{print $2}' | sort -u | xargs -r kill -9
```

I asked copilot to explain the suggestion because I wasn't very familiar with the `xargs` command. See more in the video example below:

![kill processes with open files that were deleted](https://github-production-user-asset-6210df.s3.amazonaws.com/47188731/283871467-aa563010-30a4-412b-b127-db07d3bc2e9a.gif)

Once I ran the suggested command, I was able to run the command `lsof | grep '(deleted)' | awk '{print $2}' | sort -u` to see the processes that were killed.

## 4. Exit Vim 😅
This would not be a terminal demo if I didn't ask this one question 😂:

```bash
gh copilot suggest 'how to exit vim' -t shell
```
I was provided with the following suggestion:

```bash
:q!
```

Though this is correct, it's not quite what I was looking for, because while this will take you out of vim, you will loose all your changes in the process. So I asked copilot to revise the command:

```bash
? How should this be revised?
> how to exit vim and save changes

Suggestion:

  :wq
```

Perfect! 💃🏼
See this entire flow in the visual example below:

![copilot tells us how to exist vim](https://github-production-user-asset-6210df.s3.amazonaws.com/47188731/283871777-1469f589-3913-44b9-94f7-a6c21bd44b22.gif)

# Conclusion

GitHub Copilot in the CLI is a game-changer that is super useful for reminding you of commands, teaching you new commands or explaining random commands you come across find online.

I hope you enjoy using it! Try it yourself and let me know how you used it. If you have feedback or issues, you can submit an issue in the [gh-copilot repo](https://github.com/github/gh-copilot) or leave a comment below and I'll be sure to get you an answer.

Have you tried the GitHub Copilot in the CLI? What do you think? Let me know in the comments below! 👇🏼🚀

{% twitter 1725549992792797304 %}