在 CLI 中开始使用 GitHub Copilot🚀 在 CLI 中使用 Copilot 的示例 结论

2025-06-05

在 CLI 中开始使用 GitHub Copilot

在 CLI 中使用 Copilot 的示例

结论

如果您曾经使用过终端,您就会知道尝试记住命令的困难、担心使用在互联网上找到的随机命令破坏系统或尝试找到正确的命令来执行您想要的操作的挫败感。

如今,有了CLI 中的 GitHub Copilot,这一难题就轻松解决了!💃🏼

黑人曼斯跳舞并说耶!

CLI 中的 GitHub Copilot 在终端中提供了一个类似聊天的界面,允许您询问有关命令行的问题。

副驾驶 CLI

它旨在帮助您使用常规 Shell 命令、git 命令和gh cli 命令。本文旨在帮助您安装并运行 copilot cli。

安装

要在 CLI 中安装 copilot,您必须首先安装GitHub CLI并在 OAuth 浏览器窗口中完成身份验证。如果您不熟悉 GitHub CLI,请阅读这篇博文学习如何使用它!
由于我使用的是 macOS,因此我使用 Homebrew 作为包管理器:



brew install gh
gh auth login


Enter fullscreen mode Exit fullscreen mode

您还需要拥有有效的GitHub Copilot 订阅才能在命令行中使用 copilot。安装完成
gh cli,您可以运行以下命令安装扩展:



gh extension install github/gh-copilot


Enter fullscreen mode Exit fullscreen mode

你已经准备好出发了!🎉

可用命令

我建议运行 help 命令来了解如何在命令行中与 copilot 交互。运行时,gh copilot --help您将看到以下输出:



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

您可以使用两个主要命令与 cli 中的 copilot 进行交互:gh copilot explaingh copilot suggest

创建别名💡

💡 热门提示!创建别名将使在 cli 中使用 copilot 成为常规工作流程的一部分变得更加容易。

安装完成后,我建议你接下来创建一个别名。这将使你在命令行中与 Copilot 的交互更加顺畅,因为它可以节省一些按键,让你更快地获得帮助。

由于我使用的是zsh,因此我运行了以下命令来创建别名:



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

现在,让我们了解这些命令的含义以及如何使用它们的一些示例。

💡 记住,我有一个别名,所以我将使用gceorgcs而不是gh copilot explainor gh copilot suggest

解释命令🤖

您可以通过两种方式使用 copilot cli explain 命令:

  1. 您可以不带参数运行 explain 命令,它将提示您输入要解释的命令。


gh copilot explain


Enter fullscreen mode Exit fullscreen mode

副驾驶解释快速流程

  1. 您可以同时运行 explain 命令和您想要解释的命令:


gh copilot explain 'your query'


Enter fullscreen mode Exit fullscreen mode

副驾驶解释命令

您还可以运行带有标志的 explain 命令-h来获取更多信息:



gh copilot explain -h


Enter fullscreen mode Exit fullscreen mode

副驾驶解释命令

建议命令🤖

您可以通过 3 种方式与 cli 中的 copilot 交互以获取建议:

  1. 您可以运行建议命令并按照屏幕上的提示进行操作:


gh copilot suggest


Enter fullscreen mode Exit fullscreen mode

副驾驶提出指令

  1. 您可以同时运行建议命令和查询,然后选择命令类型:


gh copilot suggest 'command you want to run'


Enter fullscreen mode Exit fullscreen mode

副驾驶提出指令

  1. 您可以同时运行建议命令、您的查询和命令类型:


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


Enter fullscreen mode Exit fullscreen mode

副驾驶提出指令

-t标志表示命令的类型,可以是以下之一:

  • git
  • gh

修改建议的命令

有时,您在命令行中从 Copilot 获得的建议并不完全符合您的需求。您可以在Revise command收到建议时选择选项来修改命令,并向 Copilot 提供更多有关您所需内容的详细信息:

副驾驶提出指令

💡 温馨提示!CLI 中的 Copilot 并非简单地提供您想要的建议。请记住,它是您的副驾驶,因此,如果输出结果不符合您的预期,请编辑建议或请求修改,直到您获得所需的结果。

解释建议的命令

当您在 cli 中获得来自 copilot 的建议时,您可以通过选择以下Explain command选项要求 copilot 解释该命令:

副驾驶提出指令

复制建议的命令

当您在 cli 中收到来自 copilot 的建议时,您可以复制提供的响应并在终端中运行它。

评级命令🌟🌟🌟🌟🌟

当您在 cli 中获得来自 copilot 的建议时,您可以通过选择Rate command选项来对命令进行评级,然后选择它是否有用:

副驾驶提出指令

退出提示流窗口🚪

您可以通过选择以下选项退出提示流窗口Exit

副驾驶提出指令

在 CLI 中使用 Copilot 的示例

现在让我们看一些例子。💃🏼

1. 解释一个危险的命令⚠️

因此,我在网上看到了这个命令,并且很好奇它的作用,所以我可以问:



gh copilot explain chmod -r 777 /


Enter fullscreen mode Exit fullscreen mode

副驾驶解释命令

💡 温馨提示!当要求副驾驶解释包含特殊字符的命令时,请将命令括在字符串中,以便副驾驶能够更好地理解。

如你所见,这个命令实际上非常危险,可能会导致你丢失所有数据。我很喜欢收到的解释,因为它不仅告诉了我这个命令的作用,还告诉我它为什么危险。

🚨切勿在您的机器上运行此命令🚨

2. 本地和远程删除 git 分支

在下面的可视化示例中,我首先使用命令列出本地仓库中的分支,git branch然后使用命令列出远程仓库中的分支git branch -r。然后,我在命令行中向 copilot 寻求建议:



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


Enter fullscreen mode Exit fullscreen mode

本地和远程删除 git 分支

我得到了以下建议:



git push origin --delete <branch_name>

git branch -D <branch_name>


Enter fullscreen mode Exit fullscreen mode

我运行了建议的命令,然后再次列出本地和远程仓库中的分支,以确认该分支已被删除。谢天谢地,它确实被删除了。🙌🏼

查看 repo 中的所有未解决的问题

你也可以在命令行中向 copilot 发出gh命令。例如,如果你想查看某个仓库中所有未解决的问题,你可以在命令行中向 copilot 发出建议:



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


Enter fullscreen mode Exit fullscreen mode

我得到了以下建议:



gh issue list --state='open'


Enter fullscreen mode Exit fullscreen mode

我运行了该命令,并获得了我当前所在的 repo 中所有未解决问题的列表。请参阅下面的可视化示例中的流程:

查看 repo 中的所有未解决的问题

3. 终止打开了已被删除文件的进程

命令行界面 (CLI) 中的 Copilot 也适用于终端中的管理任务。例如,如果你想要终止打开了已删除文件的进程,你可以在命令行界面中向 Copilot 寻求建议:



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


Enter fullscreen mode Exit fullscreen mode

我得到了以下建议:



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


Enter fullscreen mode Exit fullscreen mode

由于我对这个指令不太熟悉,我请副驾驶解释一下这个建议xargs。更多内容请见下方视频示例:

终止打开了已删除文件的进程

一旦我运行了建议的命令,我就可以运行该命令lsof | grep '(deleted)' | awk '{print $2}' | sort -u来查看被终止的进程。

4. 退出 Vim

如果我不问这个问题的话,这将不是一个终端演示😂:



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


Enter fullscreen mode Exit fullscreen mode

我得到了以下建议:



:q!


Enter fullscreen mode Exit fullscreen mode

虽然这是正确的,但这并不是我想要的,因为这会让你退出 Vim,但你会丢失所有更改。所以我让 Copilot 修改了命令:



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

Suggestion:

  :wq


Enter fullscreen mode Exit fullscreen mode

完美!💃🏼
完整流程请见下方示例:

副驾驶告诉我们如何生存 vim

结论

CLI 中的 GitHub Copilot 是一个游戏规则改变者,它对于提醒您命令、教您新命令或解释您在网上遇到的随机命令非常有用。

希望你喜欢它!亲自尝试一下,然后告诉我你的使用感受。如果你有任何反馈或问题,可以在gh-copilot 仓库中提交问题,或者在下方留言,我一定会给你答复。

你在命令行界面 (CLI) 中尝试过 GitHub Copilot 吗?你觉得怎么样?在下面的评论区留言告诉我吧!👇🏼🚀

文章来源:https://dev.to/github/stop-struggling-with-terminal-commands-github-copilot-in-the-cli-is-here-to-help-4pnb
PREV
GitHub Actions 中的 GITHUB_TOKEN:工作原理、更改权限、自定义
NEXT
GitHub 上的六个秘密彩蛋