GitHub CLI is a command-line tool that brings pull requests, issues, GitHub Actions, and other GitHub features to the terminal. GitHub CLI is an open source tool for using GitHub from your computer's command line.
Allows to:
- View, create, clone, and fork repositories.
- Create, close, edit, and view issues and pull requests.
- Review, diff, and merge pull requests.
- Run, view, and list workflows.
- Create, list, view, and delete releases.
- Create, edit, list, view, and delete gists.
- List, create, delete, and connect to a codespace.
- Retrieve information from the GitHub API.
The easiest way to check if CLI is installed is by tiping gh into the terminal.
The first step is install it.
On Windows can be done downloading (and upgrading) it from https://cli.github.com/ or just by using
winget install --id GitHub.cli
winget upgrade --id GitHub.cliOn linux run:
brew install gh
brew upgrade ghAdd the following to your devcontainer file:
"features": {
"ghcr.io/devcontainers/features/github-cli:1": {}
}We have to login using the CLI, to authenticated the user it can be done via ssh or token.
Note
To perform all the actions wanted make sure the (PAT) token has the permissions required or instead use SSH.
This command will open the menu for login.
gh auth loginUsing the CLI tool actions on the repositories can be done, this are the most usefull.
To see a list with all the repositories use:
gh repo listTo perform actions on a github repository, it can to be set up as default.
gh repo set-defaultA repo can be created using CLI, completing the required fields through the display menu.
gh repo createDisplay the description and the README of a GitHub repository.
Any repository content can be access and see using.
gh repo view <repository_name>With no argument, the repository for the current directory or the default repository content can be access and see using.
gh repo viewUse the flag -b to see a specific branch of the repository.
gh repo view <repository_name> -b <branch_name>Use the flag -w the repository is open on the browser.
gh repo view <repository_name> -wA repository can be cloned localy by using
gh repo clone <repo_name_or_URL>If the current repo is required to be deleted use.
gh repo deleteOr any repository can be delete by passing the name
gh repo delete <repository_name>To confirm deletion without prompting use flag --yes
gh repo delete <repository_name> --yesA repository can be edited using de CLI.
The repositories can be personal or from an organization (if you have permissions). To the define it set the <owner_name>
A panel with the options to edit the repository will display with.
gh repo edit <owner_name>/<repository_name>Some usefull flag are:
Use --allow-forking in a organization repository
gh repo edit <owner_name>/<repository_name> --allow-forkingUse --default-branch <name> to set the default branch name for the repository
gh repo edit <owner_name>/<repository_name> --default-branch <branch_name>With --visibility <string> the visibility of the repository can be change the posibilies are public,private,internal
gh repo edit <owner_name>/<repository_name> --visibility <option_selected>Multiple flags can be combined like, enable wiki in the repository with --enable-wiki, enable discussions in the repository with --enable-discussions and enable issues in the repository with --enable-issues.
gh repo edit <owner_name>/<repository_name> --enable-wiki --enable-discussions --enable-issuesTo toggle a setting off, use the --<flag>=false syntax.
gh repo edit <owner_name>/<repository_name> --enable-auto-merge=falseA local repository can be syncronized from a remote repo.
If it is not detailed the current reposity is the syncronized.
gh repo syncOr it can be specified.
gh repo sync <repository_name>If the syncronization is required in a specific branch use -b <branch_name> flag
gh repo sync -b <branch_name>To rename a repository use by default it will apply to the current repository
gh repo rename <new_name> To use in any repostory use the flag -R.
gh repo rename <new_name> -R <owner_name>/<repo_name>Gist can be access by cli, allowing to manage, create, delete and see this.
A local copy of a remote gist can be done by cloning it with
gh gist clone <URL or ID>The gist from an account with the ID, descrition, files, visibily and update can be listed using.
gh gist listTo see a gist locally (without cloning) use
gh gist view <ID>To open the gist in the web browser use the -w flag
gh gist view <ID> -wThe CLI allows to see, work and edit with labels.
To see the current list of labels use.
gh label listTo create a new label. Must specify name for the label.
The description is set with -d flag and color with -c (both are optional). If a color isn't provided, a random one will be chosen. The label color needs to be 6 character hex value.
gh label create <label_name> -d "description text" -c <hex_color>To update name, description and/or color from an existing label use.
The name is set with -n flag, description is set with -d and color with -c. Not allways all need to be updated. They can be choosen.
gh label edit <label_name> -n <new_label_name> -d "new description" -c <new_hex_color>Delete a label from a repository
gh label delete <label_name>The Github CLI allows to manage and see issues.
The info show by the issue list is ID, title, labels, update. To check the open issues list in the current repository run.
gh issue listThe -R flags allow to work with a specific repository.
gh issue list -R <owner_name>/<repo_name>By default only open issues are display, to see closed ones use -s <state> flag. Stated can be closed, open, all.
gh issue list -s closeIssues can be filtered by one or multiple labels using -l flag
open, all`
gh issue list -l <label_name> -l <label_name> Using status can be chech the issues assigned to you, the issues mentioning you and the issues opened by you in a repo.
gh issue statusDisplay the title, body, and other information about an issue.
gh issue view <ID>Using the -w flag it will be opened in the browser.
gh issue view <ID> -wTo close and issue using the cli follow the syntax. Where -c flag enable to leave a comment and -r to choose the reason which can be completed,not planned.
gh issue close <ID> -c "Comment" -r <reason>Working with GitHub pull requests using the CLI.
By default it will be executed in the actual repo, it can be changed using the -R flag in any command detailing the <owner_name>/<repo_name>
Info like ID, title, branch and created add will be display using.
gh pr listBy defualt only open PR are displayed. But using the -s flag this states can be selected open,closed,merged,all
gh pr list -s <state>The filter can be done by head branch with -H flag.
gh pr list -H <branch_name>It's posible to check current status of open PR with.
gh pr statusThis command will open the menu for creating a PR from the current branch into main.
The ID, name of the PR and branch will be display.
gh pr createFor closing PR run the command using the syntax.
gh pr close <ID>Optionally using the -c flag a comment can be writte and/or deleting the branch after closing -d.
gh pr close <ID> -c "Optional comment" -dAllow to swich (mark) a PR as ready for review, or as draft (work in progress)
If the PR is draft now, tranform in ready for review with.
gh pr ready <ID>Using the --undo flag can convert a PR as draf (work in progress)
gh pr ready <ID> --undoIn order to accept and merge a PR the easiest way it's by using the next command, that will open the menu for merging a specific PR.
gh pr merge <ID>To see the list organizations for the authenticated user.
gh org listTo see info about a command or an specific options type.
gh help <commnad>