Working with the command line is something many developers love. Even though we love it, there are times when it can be really frustrating. What’s the command for switching to a branch? How do I fix merge conflicts? Do I have the correct credentials or permissions for my file or directory?
In our recent blogs, we showed you some of the top Git commands and useful commands for the GitHub CLI. However, there are hundreds, if not thousands, of terminal-based commands, and knowing them all would be difficult. We could search for the correct command in a browser but at the cost of breaking our flow and maybe still not finding exactly what we need.
In the previous blog, we showed you how to use –help to receive some helpful suggestions about which commands to use, but this is usually a basic list. Instead, wouldn’t it be great if we could have a conversation with our terminal and ask which commands to use? This is where GitHub Copilot in the CLI comes into play.
GitHub Copilot in the CLI
Many developers are loving GitHub Copilot Chat, and the time-saving benefits and productivity gains that come with it. So, we thought, “Why not bring GitHub Copilot to the command line?†With GitHub Copilot in the CLI, we can ask questions to help us with our terminal tasks, whether they are Git-related commands, GitHub, or even generic terminal commands.
If this sounds like something you want to try, then read on. We’ve also left you with some challenges for you to try yourself.
Getting started
To get started, you’ll need to make sure you have the GitHub CLI installed on your Windows, Mac, or Linux machine, and an active subscription of GitHub Copilot. If your Copilot subscription is part of a Business or Enterprise license, you’ll need to ensure your organization or enterprise’s policy allows you to use “Copilot in the CLI:â€
You can find these Copilot settings by clicking your profile icon in the top right-hand corner on github.com → Settings → Copilot.
Ensure you’re authenticated in your terminal with GitHub by using gh auth login. You can follow the guide in our CLI Docs to ensure you authenticate correctly.
Copilot in the CLI is a GitHub CLI extension. Thus, you have to install it by typing gh extension install github/gh-copilot into your terminal of choice. Since I’m using Windows, all the examples you see here will be Windows PowerShell:
Now that you have Copilot in your CLI, you can use gh copilot to help you find information you are looking for. Let’s look at some of the most common things you can do with Copilot.
Have GitHub Copilot explain computer science concepts
GitHub Copilot is your friend when it comes to using the terminal, regardless of how familiar you are. Copilot can help explain something by using gh copilot explain, followed by a natural language statement of what you want to know more about. As an example, you might like to know how to roll back a commit:
You can receive help from Copilot when you don’t understand exactly what a particular command does. For example, a teammate recently passed me the script npx sirv-cli . to run in my terminal as part of a project we were working on. If I want to better understand what this command does, I can ask Copilot:
TRY IT: Ask Copilot to explain the difference between Git and GitHub.
If you get stuck, you can type gh copilot –help to see a list of commands and examples for how to use Copilot in the CLI:
GitHub Copilot can suggest commands
Explaining concepts is good for understanding and knowledge. When we want to execute a command, however, the explain command might not be enough. Instead, we can use the suggest command to have GitHub Copilot suggest an appropriate command to execute. When it comes to suggesting commands based on your questions, Copilot will follow up with another question, such as “What kind of command can I help you with?†with three options for you to choose from:
The user can choose between:
generic shell command (terminal command)
gh command (GitHub CLI command)
git command
Copilot can then provide a suggestion based on the type of command you want to use. Let’s dive into each of the three command types.
Generic shell commands
There are hundreds of terminal specific commands we can execute. When asking GitHub Copilot for a suggested answer, we’ll need to select generic shell command from the drop down. As an example, let’s ask Copilot how to kill a process if we’re listening on a specific port:
Along the way, we are answering the questions Copilot is providing to us to help refine the prompt. At the end of each suggestion, we are able to have Copilot explain further, execute the command, copy the command, revise the command, or exit the current question.
TRY IT: Ask Copilot how to list only *.jpg files from all subfolders of a directory.
Git commands
In our recent blog, we went through some of the main Git commands every developer should know. Instead of having to search for a specific command, you can ask GitHub Copilot for help directly from the command line.
For Git commands, select git command from the Copilot drop down. If we wanted to know which branch we were on before making a commit, we could ask Copilot to suggest the best way to achieve this:
In this example, you can see I first ask to have the answer explained, and then execute the command to see that we are currently working on the main branch.
What if we accidentally checked our new changes onto the main branch, but we actually want them on a new branch? We can ask Copilot how would we go about fixing this:
Remember to also check the responses Copilot is giving you. In the above example, Copilot gave me a very long answer to my question, and my question was also rather long. We didn’t get exactly what I needed. Instead, I select to revise the question further. Still, we didn’t get exactly what we wanted. Let’s revise it again, and ask to add the changes to a new branch instead:
Now, I have three steps to execute in order to create a new branch, reset the previous branch, and then switch to the new branch. From there, I can make a new commit.
Once we’ve made a commit, we can ask how to update the previous commit message:
Now, we can change the previous commit message.
TRY IT: Ask Copilot how to merge a pull request as an admin.
GitHub commands
In our last blog, we showed you useful GitHub commands for using the GitHub CLI. Now, let’s ask GitHub Copilot when we get stuck. When we want to ask Copilot about GitHub- specific commands, choose the gh command from the drop down menu in the terminal.
Let’s look at diffs—the difference between two files or two commits on GitHub. We can ask Copilot how to view these differences. Here, I’m also asking Copilot in the terminal via VS Code, and it’s also providing me with suggestions for the question:
Here, I didn’t specify in the prompt whether it was a GitHub command. By choosing gh command, Copilot knows I am looking for a GitHub-specific command, and therefore shows me the command for showing a difference of the pull request number we selected.
Now, let’s see if there are any pull requests or issues from this repository that are assigned to me:
Copilot tells me there are none assigned to me from this repository—winning!
Let’s put a few Git and GitHub commands together. Let’s ask how to open a pull request from a branch using a gh command. Firstly, let’s ask Copilot to commit all my changes to a new branch, and then ensure I’m on the correct branch. After switching to the correct brand, we can ask Copilot how to open a pull request from a branch we are on:
Remember (again) to check the responses we are given. In this example, Copilot gave us the command gh pr create –base –head –title Ҡ–body <”pull_request_body>” which provides all the tags. If we just use gh pr create then we are guided through the pull request process. We can follow the prompts within the command line and ask Copilot along the way for help if we get stuck. I created a draft pull request so I can work on it with my team further before converting it to an open pull request.
By answering the questions Copilot gives us, such as “What kind of command†is this, and selecting the correct option, we can have Copilot successfully execute a command. In this case, we have committed our code to a new branch, navigated to the correct branch, and opened the pull request as a draft.
TRY IT: Ask Copilot how to create a new release and edit the contents.
Working with aliases
All this typing gh copilot suggest has got me thinking “there’s got to be a faster way to use GitHub Copilotâ€, and there is. We can use the prebuilt ghcs alias to have “Copilot suggest†a command for us. We’ll need to configure GitHub Copilot in the CLI before we can use these commands. There are also flags like –target (or -t for short), which allow us to specify a target for the suggestion, such as shell, gh, or git. In this way, we can make our conversation with Copilot so much faster. To learn more about the commands and flags available, you can use –help with any Copilot command or either of the ghce and ghcs aliases.
Each system configures these aliases differently. Check out the Copilot Docs and video for how to configure aliases for you.
TRY IT: Configure Copilot in the CLI with aliases for even fewer keystrokes.
Using GitHub Copilot CLI
When it comes to using GitHub Copilot in the CLI, the question you ask–also called the prompt–is really important for receiving an answer that is correct for your situation. Unlike GitHub Copilot in your editor, Copilot CLI doesn’t have as much context to draw from. You’ll need to ensure the prompt you write is succinct, and captures the question you are wanting to ask. If you want some tips on writing good questions, check out our guide on prompt engineering. You can always revise your question to get the answer you are looking for.
This has been a brief introduction on using Copilot from the command line. Now, you’re ready to give our “try it†examples a go. When you try these out, share your results in this discussion so we can see the answers Copilot gives you and discuss them together.
The post Boost your CLI skills with GitHub Copilot appeared first on The GitHub Blog.
Source: Read MoreÂ