【公式ドキュメントを読もうシリーズ】コマンド ラインでの GitHub Copilot の使用
シリーズ概要
https://qiita.com/yasu_programming/items/cec66f9e8d5d571e9ce3
URL
https://docs.github.com/ja/copilot/using-github-copilot/using-github-copilot-in-the-command-line
説明
コマンドラインでGithub Copilotを使用する方法が書かれている。
手順は下記の通りです。
手順
- ghコマンドのインストール
- Github CLIにログイン
- Copilot in the CLI 拡張機能のインストール
- コマンドラインでのGithub Copilotを試してみる
ghコマンドのインストール
そもそもなんでghコマンドというコマンドをインストールするのかというと、コマンドラインでGithub Copilotを使用するうえで必要だからです(そりゃそうか)。
gh はgithub の略なのだろう。
これも公式のドキュメントありました。
https://docs.github.com/ja/github-cli/github-cli/about-github-cli
私はubuntuで触っていたので、こちらのリンクhttps://github.com/cli/cli/blob/trunk/docs/install_linux.mdのコマンドを叩いて、ghコマンドをインストールしました。
公式ドキュメントからのリンクですので、それぞれの環境に応じたghコマンドのインストール方法を探してください。
Github CLIにログイン
ログイン方法も公式リンクhttps://docs.github.com/ja/copilot/managing-copilot/configure-personal-settings/installing-github-copilot-in-the-cliから飛ぶことができます。
gh auth login
? Where do you use GitHub? GitHub.com
? What is your preferred protocol for Git operations on this host? SSH
? Upload your SSH public key to your GitHub account? Githubの公開鍵のパス
? Title for your SSH key: Githubの秘密鍵のパス
? How would you like to authenticate GitHub CLI? Login with a web browser
! First copy your one-time code: ****-****
Press Enter to open https://github.com/login/device in your browser...
Press Enterすると、Github のログイン画面に遷移し、codeを入力すると、ログイン完了になる。
Copilot in the CLI 拡張機能のインストール
公式ドキュメントに沿って、インストールしてみる。
$ gh extension install github/gh-copilot
✓ Installed extension github/gh-copilot
$ gh extension upgrade gh-copilot
[copilot]: already up to date
✓ Successfully checked extension upgrades
できた模様。
コマンドラインでのGithub Copilotを試してみる
公式ドキュメントのコマンド叩いてみる。
$ gh copilot explain "sudo apt-get"
? Allow GitHub to collect optional usage data to help us improve? This data does not include your queries.
> Yes
Welcome to GitHub Copilot in the CLI!
version 1.1.0 (2025-02-10)
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, allowing changes to system files.
• apt-get is a command-line tool for handling packages in Debian-based systems, such as Ubuntu.
• Available sub-commands include:
• update to refresh the list of available packages.
• upgrade to upgrade installed packages to their latest versions.
• install to install a new package.
• remove to uninstall a package.
• autoremove to remove packages that were automatically installed to satisfy dependencies for other packages and are no longer
needed.
• You can leverage apt-get --help for more information on available options and usage.
答えてくれた。
日本語入れたらあんまり答えてもらえないみたい(2025年5月10日現在)。
フィードバックしたら、改善するみたいですので、フィードバックしたほうが良いのかもしれない。
$ gh copilot explain "github cliでできることを教えてほしい"
Welcome to GitHub Copilot in the CLI!
version 1.1.0 (2025-02-10)
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 not readily available. Please revise for better results.
--help
で何できるか聞いてみた。
gh copilot --help
Your AI command line copilot.
Usage:
copilot [command]
Examples:
$ gh copilot suggest "Install git"
$ gh copilot explain "traceroute github.com"
Available Commands:
alias Generate shell-specific aliases for convenience
config Configure options
explain Explain a command
suggest Suggest a command
Flags:
-h, --help help for copilot
--hostname string The GitHub host to use for authentication
-v, --version version for copilot
Use "copilot [command] --help" for more information about a command.
いろいろ試したいね。
所感
gh copilot
コマンドいろいろできそうだったので、alias
config
explain
suggest
試したいです。
コードに個人情報が含まれていないか、確認するのが、地味に大変だなと思った。