TL;DL
GitHub CLI を使うと以下のコマンドで GitHub リポジトリの URL を開ける。
1$ gh browse
GitHub リポジトリの URL をターミナルから開く
ターミナル上で作業していると GitHub の URL を直ぐに開きたいことがある。そのような時は、GitHub であれば gh コマンド (GitHub CLI) を使うことで直接ブラウザで URL を開ける。
1$ gh browse
これが機能する条件は、リモートリポジトリを設定していることだ。 GitHub をリモートリポジトとして使っているのであれば、基本的には以下のコマンドをリポジトリを作成したときに実行しているか、
1$ git remote add origin {リモートリポジトリ}
先に GitHub 上でリポジトリを作成し、クローンしてきていると思うので設定されているだろう。リモートリポジトリが設定されているかは次のコマンドで確認できる。
1$ git remote -v
何故この記事を書いたか
gh コマンドで GitHub リポジトリが開けたはずだけどサブコマンドは何だったかな?と思ってそれらしいキーワードでググってみたところ、一番シンプルな解決方法がヒットしなかった。
gh browse 以外の方法として、
- git コマンドのエイリアス
- bash/zsh のエイリアス
hubコマンドのhub browseghコマンドのrepoサブコマンドを使ったgh repo view --web
あたりがヒットする。昔は gh repo view --web 以外に方法がなかったのかもしれないが、現在は gh browse でリポジトリに限らず、
GitHub の様々なページをターミナル上から開ける。
そんなわけで、ターミナルから GitHub を開く方法を調べた人の知識のアップデートになればと思い記事に残しておく。
gh browse
1$ gh browse --help2Transition from the terminal to the web browser to view and interact with:34- Issues5- Pull requests6- Repository content7- Repository home page8- Repository settings91011USAGE12gh browse [<number> | <path> | <commit-sha>] [flags]1314FLAGS15-a, --actions Open repository actions16-b, --branch string Select another branch by passing in the branch name17-c, --commit string[="last"] Select another commit by passing in the commit SHA, default is the last commit18-n, --no-browser Print destination URL instead of opening the browser19-p, --projects Open repository projects20-r, --releases Open repository releases21-R, --repo [HOST/]OWNER/REPO Select another repository using the [HOST/]OWNER/REPO format22-s, --settings Open repository settings23-w, --wiki Open repository wiki2425INHERITED FLAGS26--help Show help for command2728ARGUMENTS29A browser location can be specified using arguments in the following format:30- by number for issue or pull request, e.g. "123"; or31- by path for opening folders and files, e.g. "cmd/gh/main.go"; or32- by commit SHA3334EXAMPLES35# Open the home page of the current repository36$ gh browse3738# Open the script directory of the current repository39$ gh browse script/4041# Open issue or pull request 21742$ gh browse 2174344# Open commit page45$ gh browse 77507cd94ccafcf568f8560cfecde965fcfa634647# Open repository settings48$ gh browse --settings4950# Open main.go at line 31251$ gh browse main.go:3125253# Open main.go with the repository at head of bug-fix branch54$ gh browse main.go --branch bug-fix5556# Open main.go with the repository at commit 775007cd57$ gh browse main.go --commit=77507cd94ccafcf568f8560cfecde965fcfa635859ENVIRONMENT VARIABLES60To configure a web browser other than the default, use the BROWSER environment variable.6162LEARN MORE63Use `gh <command> <subcommand> --help` for more information about a command.64Read the manual at https://cli.github.com/manual65Learn about exit codes using `gh help exit-codes`66Learn about accessibility experiences using `gh help accessibility`
