Getting Started with GitHub Copilot in the CLI🚀

Kedasha - Nov 17 '23 - - Dev Community

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! 💃🏼

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

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 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!
Since I'm on macOS, I used homebrew as my package manager:



brew install gh
gh auth login


Enter fullscreen mode Exit fullscreen mode

You also need to have an active GitHub Copilot subscription 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:



gh extension install github/gh-copilot


Enter fullscreen mode Exit fullscreen mode

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:



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.



Enter fullscreen mode Exit fullscreen mode

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, I ran the following commands to create aliases:



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


Enter fullscreen mode Exit fullscreen mode

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.


gh copilot explain


Enter fullscreen mode Exit fullscreen mode

copilot explains prompt flow

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


gh copilot explain 'your query'


Enter fullscreen mode Exit fullscreen mode

copilot explains a command

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



gh copilot explain -h


Enter fullscreen mode Exit fullscreen mode

copilot explains a command

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:


gh copilot suggest


Enter fullscreen mode Exit fullscreen mode

copilot suggests a command

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


gh copilot suggest 'command you want to run'


Enter fullscreen mode Exit fullscreen mode

copilot suggests a command

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


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


Enter fullscreen mode Exit fullscreen mode

copilot suggests a command

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

💡 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

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

Exiting the prompt flow window 🚪

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

copilot suggests a command

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:



gh copilot explain chmod -r 777 /


Enter fullscreen mode Exit fullscreen mode

copilot explains a command

💡 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:



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


Enter fullscreen mode Exit fullscreen mode

Delete a git branch locally and remotely

I was provided with the following suggestion:



git push origin --delete <branch_name>

git branch -D <branch_name>


Enter fullscreen mode Exit fullscreen mode

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:



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


Enter fullscreen mode Exit fullscreen mode

I was provided with the following suggestion:



gh issue list --state='open'


Enter fullscreen mode Exit fullscreen mode

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

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:



gh copilot suggest 'kill processes with open files that have been deleted' -t shell


Enter fullscreen mode Exit fullscreen mode

I was provided with the following suggestion:



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


Enter fullscreen mode Exit fullscreen mode

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

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 😂:



gh copilot suggest 'how to exit vim' -t shell


Enter fullscreen mode Exit fullscreen mode

I was provided with the following suggestion:



:q!


Enter fullscreen mode Exit fullscreen mode

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:



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

Suggestion:

  :wq


Enter fullscreen mode Exit fullscreen mode

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

copilot tells us how to exist vim

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 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! 👇🏼🚀

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .