GitHub Copilot CLI 是一个命令行工具,它允许你在终端中使用 GitHub Copilot。你可以使用它来获取代码建议,这些建议是由 OpenAI 的 GPT-4 模型生成的。这个工具可以在任何支持命令行的环境中使用,包括 Visual Studio Code 的集成终端。

参考官方文档:Using GitHub Copilot in the CLI - GitHub Docs

安装

前提:

  1. 需要订阅Github Copilot,每月$10。

  2. 需要安装GH CLI

订阅自行解决,接下来安装GH CLI:

sudo mkdir -p -m 755 /etc/apt/keyrings && wget -qO- https://cli.github.com/packages/githubcli-archive-keyring.gpg | sudo tee /etc/apt/keyrings/githubcli-archive-keyring.gpg > /dev/null \
&& sudo chmod go+r /etc/apt/keyrings/githubcli-archive-keyring.gpg \
&& echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" | sudo tee /etc/apt/sources.list.d/github-cli.list > /dev/null \
&& sudo apt update \
&& sudo apt install gh -y

如果没法安装,可以下载deb文件手动安装。Release GitHub CLI 2.46.0 · cli/cli

安装完GH CLI后,安装Copilot CLI:

$ gh auth login
# 选择登录账号类型为Github.com
? What account do you want to log into?  [Use arrows to move, type to filter]
> GitHub.com
  GitHub Enterprise Server
# 选择传输协议为SSH
? What is your preferred protocol for Git operations on this host?  [Use arrows to move, type to filter]
  HTTPS
> SSH
# 选择一个本地的公钥,并命名SSH key的名称
? Title for your SSH key: (GitHub CLI) wsl2
# 选择验证方式为使用浏览器验证
? How would you like to authenticate GitHub CLI?  [Use arrows to move, type to filter]
> Login with a web browser
  Paste an authentication token
! First copy your one-time code: 7AAA-SA47
# 回车后会弹出浏览器,输入验证码即可
Press Enter to open github.com in your browser...
! Failed opening a web browser at https://github.com/login/device
  fork/exec /usr/bin/winchrome: exec format error
  Please try entering the URL in your browser manually

WSL2 环境下可能会出现无法打开浏览器的情况,可以手动打开浏览器输入地址验证https://github.com/login/device。

使用

支持两个命令:

# 解释代码
gh copilot explain
# 获取命令行建议
gh copilot suggest

示例:

$ gh copilot explain "sudo apt-get"

Welcome to GitHub Copilot in the CLI!
version 1.0.1 (2024-03-22)

I'm powered by AI, so surprises and mistakes are possible. Make sure to verify any generated code or suggestions, and share feedback so that we can learn and improve. For more information, see https://gh.io/gh-copilot-transparency

Explanation:                                                                                                                                                                       
                                                                                                                                                                                   
  • sudo is used to run a command with elevated rights, typically as a superuser.                                                                                                  
    • apt-get is the Ubuntu package manager.                                                                                                                                       
      • It is used to manage packages on the system, including installing, updating, and removing software packages.                                                               
      • It requires administrative privileges to perform these operations.                                                                                                         
      • Additional sub-commands can be used with apt-get to perform specific tasks, such as install, remove, update, etc.                                                          
$ gh copilot suggest "Install git"
Welcome to GitHub Copilot in the CLI!
version 1.0.1 (2024-03-22)

I'm powered by AI, so surprises and mistakes are possible. Make sure to verify any generated code or suggestions, and share feedback so that we can learn and improve. For more information, see https://gh.io/gh-copilot-transparency

? What kind of command can I help you with?  [Use arrows to move, type to filter]
> generic shell command
  gh command
  git command
Suggestion:                                                                                                                                                                        
  sudo apt-get install git                                                                                                                                                     
? Select an option  [Use arrows to move, type to filter]
> Copy command to clipboard
  Explain command
  Execute command
  Revise command
  Rate response
  Exit

gh copilot 默认提供了这两个命令的别名,无需输入完整命令,可以直接使用:

# ghce 为 gh copilot explain 的别名
ghce "sudo apt-get"
# ghcs 为 gh copilot suggest 的别名
ghcs "install git"

为了便于在每个终端中使用 gh copilot cli,将以下配置放到配置文件中:

echo 'eval "$(gh copilot alias -- zsh)"' >> ~/.zshrc