Exploring GitHub Copilot X: AI-Powered CLI Commands and More

GitHub recently released Copilot X, which brings even more AI-powered goodness to the tools you use beyond the code editor. This includes being able to use Copilot for automatically adding tags to your pull requests or AI-powered docs that scope your searches to the actual documentation that you’re trying to look up answers for, including some of the demos they have available like React and MDN. In this post, we’ll dive into the GitHub Copilot CLI, which allows you to build commands using AI.

What's Inside

YouTube Preview
View on YouTube

GitHub Copilot X Overview and Installation

The original GitHub Copilot has already broken a lot of ground for what it was able to do by bringing AI into your tools and has made a significant impact worldwide, helping people and businesses build better and faster. While it seems like there’s a new GPT model every week, Copilot X uses GPT-4, allowing us to take advantage of the latest from OpenAI.

To get started with Copilot for CLI, you’ll first need to sign up for the waitlist.

Next, install the GitHub Copilot CLI globally using npm and authenticate via the GitHub Copilot CLI auth.

npm install -g @githubnext/github-copilot-cli
github-copilot-cli auth

After authentication, add the following aliases to your environment to make it easier to use the different tools:

alias ??='copilot-cli query'
alias git?='copilot-cli query --tool git'
alias gh?='copilot-cli query --tool gh'

Note: To use gh?, you must have the GitHub CLI installed

Putting Copilot CLI to the Test

With everything installed and configured, let’s take Copilot CLI for a spin by running several tests.

Copilot essentially provides three different methods for how you can interact with it:

  1. ?? for any shell command
  2. git? for Git-specific commands
  3. gh? for GitHub CLI commands

Listing Files in a Directory

For a simple test, let’s list all the files in a directory using the ?? command:

?? list all of the files in this directory
Listing files in a directory

Running the command confirms the query and lists the files as expected.

Renaming Multiple Files

For a slightly more complicated task, let’s rename all files in a directory by replacing a specific word.

?? rename all of the files to replace "video" with "solid"
Renaming a list of files

Copilot generates a command that loops through each file, moves them, and uses sed to replace the word “video” with “solid”. Run the command, and you’ll see that the files are now updated with the new word.

Clearing Out Node Modules Directories

As developers, we often accumulate many node_modules directories across our projects. To recursively remove all node_modules directories using Copilot, run:

?? recursively remove all node_modules directories
Recursively removing all node_modules directories

The generated command uses find to locate all node_modules directories and rm -rf to remove them.

Working with Git Commands

For those who frequently work with Git, Copilot provides a way to recall and execute Git commands more easily.

To change the message of the last Git commit, run:

git? change the message of the last commit
Change message of latest git commit

Copilot will suggest the git commit --amend command, which you can then execute and update the commit message as needed.

To view the first commit in the Git repository, run:

git? what was the first commit to this repository
Finding first commit

Copilot will generate a command using git log --reverse --oneline, which, when executed, will display the first commit in the repository.

Working with GitHub CLI Commands

For those who use the GitHub CLI, Copilot can streamline many tasks, such as managing pull requests and issues.

To view all open pull requests for a GitHub repository, run:

gh? what are the open pull requests for this repository
Finding open Github Pull Requests

Copilot will suggest using gh pr list, which will display all open pull requests when executed.

To view all closed pull requests, run:

gh? what are all of the closed pull requests for this repository
Finding closed GitHub Pull Requests

The generated command adds the --state closed flag to filter the list of pull requests accordingly.

To check out the code from a specific pull request, run:

gh? check out pull request number ###
Checking out a specific pull request

Copilot will generate a command using gh pr checkout, allowing you to check out the pull request locally.

Finally, to create a new issue on GitHub, run:

gh? create a new issue with a title "Super Important Bug" and a description "This is causing huge issues for my app (test)"
Creating a GitHub issue from the terminal

The suggested command will include the appropriate flags for the title and description, and when executed, will create the issue on GitHub.

Embracing AI in Development

While AI tools like Copilot X may not be perfect in every situation and often require some clarification, they offer developers powerful ways to streamline tasks, create better code, and ultimately build better experiences for users. These tools are unlikely to replace developers completely, but will empower us to accomplish more at a faster pace.