Notes
TL;DR
With GitHub CLI, you can open the URL of the current GitHub repository with this command:
1$ gh browse
Open a GitHub repository URL from the terminal
When working in the terminal, there are times when you want to open a GitHub URL immediately.
In that case, if the repository is on GitHub, you can use the gh command (GitHub CLI) to open the URL directly in your browser.
1$ gh browse
This works as long as a remote repository is configured. If you use GitHub as the remote repository, you have probably either run the following command when creating the repository,
1$ git remote add origin {remote repository}
or you created the repository on GitHub first and then cloned it locally, so it should already be set up. You can check whether a remote repository is configured with this command:
1$ git remote -v
Why I wrote this article
I remembered that I could open a GitHub repository with the gh command, but I forgot which subcommand it was.
So I searched with some likely keywords, but the simplest solution did not show up at the top.
Other methods besides gh browse that show up include:
- a git alias
- a bash/zsh alias
hub browsefrom thehubcommandgh repo view --webusing thereposubcommand ofgh
Maybe gh repo view --web used to be the only way, but now gh browse can open not only repositories but many kinds of GitHub pages directly from the terminal.
That is why I wanted to leave this note for anyone searching for how to open GitHub from the terminal.
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`
