DEV Community

Cover image for Getting Started with GitHub Copilot in the CLIπŸš€
Kedasha for GitHub

Posted on

Getting Started with GitHub Copilot in the CLIπŸš€

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! πŸ‘‡πŸΌπŸš€

Top comments (8)

Collapse
 
daveparr profile image
Dave Parr

Great wrote up. Had GH installed for a while but hadn't spotted this. I started building a related AI thing called starpilot a few weeks ago because I wanted a way to discover hidden gems in my GitHub stars, but maybe they're going to scoop me on it with tools like this!

Collapse
 
ladykerr profile image
Kedasha

oh that's soo cool! Starpilot looks promising Would love to have you on open source fridays if you wanna show it off!

Collapse
 
daveparr profile image
Dave Parr

I was inspired by what Simon said about blogging in this ep I watched this morning and spent some time this afternoon dusting off the old dev.to blog and wrote up a post on starpilot :)

Collapse
 
daveparr profile image
Dave Parr • Edited

Sure, what is OpenSource Fridays?

EDIT: Ha, just found what you mean. answer is still sure :)

Collapse
 
weldingtorchhh profile image
Welding-Torch

To add gcp(or any term) as your github copilot alias on Windows command prompt, do the following:

  1. Open Notepad and type in doskey gcp=gh copilot $*
  2. Save the file as aliased.cmd in C:\Windows
  3. Open regedit.msc and paste Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Command Processor into the address bar
  4. Right-click and select New String Value. Name the key AutoRun and paste C:\Windows\aliases.cmd as the key's value.

@ladykerr Maybe you can add this to the article, since Windows currently has 70% marketshare for Desktop users. I think it would be super helpful.

Collapse
 
weldingtorchhh profile image
Welding-Torch

Update: Actually don't do that because even though it works you see the alias being set every time you run cmd. Instead use bgreco.net/alias/, and after installing it type alias gcp=gh copilot in command prompt.

That's it.

Collapse
 
vulwsztyn profile image
Artur Mostowski • Edited

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 must be the most junior paragraph ever written.

This "article" is also literally about 2 subcommands.

How do you think is your "article" better than much more concise docs docs.github.com/en/copilot/github-...?

On the other hand I love the screebshots/gifs and that it is relatively "in-depth".

Collapse
 
ladykerr profile image
Kedasha

This blog is not intended to be better than the official docs. It is a guide on how to use copilot in the cli. Feel free to download the tool and ask it more advanced questions. This blog was written for devs across all experience levels in mind.