visit
Isn’t that awesome?
The motive behind this CLI is to move all of the developer workflow right to the terminal where we were already working with git. Now you might ask, will it replace the git CLI? The answer is no. The CLI is meant to integrate well with git, which means your trusty old git isn’t going anywhere. Rather this was one of the motives behind the creation of the GitHub CLI, bringing all your tools to one place to avoid context switching.
But wait! doesn’t something like this already exists? Yes, yes it does. A GitHub wrapper called which is an open-source extension to command-line git, maintained by a GitHub employee, that lets you do everyday GitHub tasks without ever leaving the terminal. Sounds pretty familiar eh? Hub already offers a lot of the things that the Github CLI brings to the table.
The GitHub team mentions that the primary reason they didn’t build on top of hub was that they didn’t want to wrestle with the 10 odd years of design decisions that hub went through which weren’t really focused on GitHub workflows. They also didn’t want to change hub significantly because that might upset the already established user base which used hub on a daily basis. They addressed this in a lot more detail in their .
So enough talk, let’s see how it actually works. By the end of this article, you should be well on your way to integrating the GitHub CLI in your workflow.gh auth login
gh auth refresh
gh auth status
gh config set editor editorName
Replace editorName with the name of your editor like emacs, vim, nano etc.
eval "$(gh completion -s bash)"
gh repo create
gh repo repoName
replace repoName with the name that you want to give to your repository.
gh repo create orgName/repoName
Replace orgName with the name of your organization and repoName with the name of the repository as usual.
-y, --confirm Confirm the submission
-d, --description "string" Description of your repo
--enable-issues Allow people to add issues(default value is true)
--enable-wiki Enable wiki in the new repository (default true)
-h, --homepage "string" Repository home page URL
--internal Make the repository internal
--private Make the repository private
--public Make the repository public
-t, --team "string" The name of the organization team to be granted access
-p, --template "string" Make the new repository based on a template repository
So for example if I want to create a repository with the name of Python under the organization Monty and I want to set it to private with issues disabled, I would enter:
gh repo create Monty/Python --private --enable-issues=false
As simple as that.
You can clone repositories more simply in gh by using its unique owner/repo syntax:
gh repo clone owner/repo
Replace owner with the account name of the owner and repo with the repository name.
You can also clone repositories using Github URLs like before:gh repo clone <code class="markup--code markup--pre-code"><a href="//github.com/cli/cli" data-href="//github.com/cli/cli" class="markup--anchor markup--pre-anchor" rel="noopener" target="_blank">//github.com/cli/cli</a></code>
gh repo fork
gh repo fork user/repo
Replace user with the username and repo with the repo name.
You can provide flags like ‘ — remote’ and ‘ — clone’ to skip prompts. The flags take a value either true or false.
gh repo fork user/repo --clone=true --remote=true
This will also clone the forked repository and also set an upstream remote, all in one line.
Now let’s talk about viewing repositories. You can view the description and README of any Github repository right in your terminal:
gh repo view user/repo
Pretty neat!
You can also choose to view the repo in your browser by providing the ‘ — web’ or the ‘ -w’ flag.gh repo view user/repo --web
gh repo view
gh gist create --public example.cpp
The public flag will make the gist public. Keep in mind that the default value is private so in the absence of a flag, your gist will be private.
To give a description to the gist use the -d flag:
gh gist create --public example.cpp -d "An example program in c++"
Add multiple files in your gist by separating them with a whitespace:
gh gist create example.cpp app.js hello.py
You can also write a gist in the terminal by using a hyphen (-) in place of the file name:
gh gist create -
gh gist list
You can also give flags like “ — public” or “ — secret” to list only your public gists or secret gists.
gh gist edit gistID
gh gist edit gistURL
gh gist list
And then copying the 32 character code displayed there or you can copy the code from the URL of your gist, it would be the last part of your URL.
But here’s something tricky...
although I recommend using gistID because of its simplicity, if you plan to use the gist URL, simply copying and pasting the URL of your gist won’t work, it will give you a 404 something like this:
Four Oh Four
To make it work you need to remove your username from the URL and then run the command.
If done correctly, your gist should be opened in the editor that you set earlier.gh gist view gistID
gh gist view gistURL
If you want to view it raw without rendering markdown put the “ — raw” flag at the end. View the gist in a browser by the “ — web” flag.
Now we are going to tackle working with pull requests using gh. Make sure that you are navigated into the project directory.
Note: As a general rule of thumb use the flag “-R owner/repo” or “ — repo owner/repo” with all of the commands below if you want to select a repository other than the one that you are navigated into.gh pr list
This will list all your open pull requests.
If you want to list the closed or merged pull requests, you can do so by adding the ‘ — state “string”’ flag as shown below:
gh pr list --state closed
To see all of the pull requests give the argument ‘all’ to the ‘state’ flag:
gh pr list --state all
<code class="markup--code markup--pre-code">-a, --assignee string Show all pull requests by an assignee<br> -B, --base string Filter by base branch<br> -l, --label strings Filter by labels<br> -L, --limit int Maximum number of items to fetch (default 30<br> -s, --state string Filter by state: {open|closed|merged|all} (default "open")<br> -w, --web Open the browser to list the pull requests</code>
gh pr view
You can give the pull request number or a URL or a branch to view a specific pull request:
gh pr view 5
gh pr view master
gh pr view urlOfPullRequest
gh pr create
Make sure that you are in the project directory and are navigated to the branch that you want to push to the origin master.The above command will display some prompts asking you to specify the title and body of your pull request. If the branch isn’t pushed to a git remote or some files in the branch haven’t been pushed then you will be prompted to enter the location where you want the branch to be pushed, gh will also give you an option to fork the base repository.If you don’t want to create a pull request interactively then you can give flags with the command to provide the information there:
gh pr create --title "Bumped to 2.0" --body "Updated the version"
Assign the pull request to someone:
gh pr create --assignee username
Request a pr review from someone:
gh pr create --reviewer username
Select a branch in which you want your code to be merged:
gh pr create --base branchName
-a, --assignee login Assign people by their login
-b, --body string Body for the pull request
-d, --draft Mark pull request as a draft
-f, --fill Skip the prompt for title/body and use commit info
-H, --head branch The branch that contains commits for your pull request (default: current branch)
-l, --label name Add labels by name
-m, --milestone name Add the pull request to a milestone by name -p, --project name Add the pull request to projects by name
-t, --title string Give the pull request a title.
-w, --web Create the pr in a web browser
gh pr checkout prURL or branch or prNumber
You can either provide the URL of the pull request or the number of the pull request or the branch.
This will navigate you to the branch of the pull request with all the files loaded in your working directory and ready to go. From here you can test to your heart’s content. You can do a quick ‘ls’ command to view the contents of the pull request.To go back to the master branch simply use git checkout.git checkout master
gh pr merge prNumber or prURL or branch
This will merge the pull request into the origin master branch, and will by default delete the head branch of the pull request from both the local and the remote repositories. If you want to retain the branch use:
gh pr merge prNumber --delete-branch=false
-m, --merge Merge the commits with the base branch
-r, --rebase Rebase the commits onto the base branch
-s, --squash Squash the commits into one commit and merge it into the base branch
gh pr close prNumber or prURL or branchName
If you want to delete the branch of the pull request from the local and the remote repository then use the “ — delete-branch” flag.
gh pr diff prNumber or prURL or branchName
gh pr status
Mark a pull request ready for review
gh pr ready prURL or prNumber of branch
Reopen a closed pull request
gh pr reopen prNumber or prURL or branch
Review a pull request
gh pr review prNumber or prURL or branch
-a, --approve Approve the pull request
-b, --body string Specify the body of a review
-c, --comment Comment on a pull request
-r, --request-changes Request changes on a pull request
gh issue list //list all issues
-a, --assignee string Filter by assignee
-A, --author string Filter by author
-l, --label strings Filter by labels
-L, --limit int Maximum number of issues to fetch (default 30)
--mention string Filter by mention
-m, --milestone number Filter by milestone number or `title`
-s, --state string Filter by state: {open|closed|all} (default "open")
-w, --web Open the browser to list the issue(s)
Viewing an issue
gh issue view issueNumber or issueURL
Creating an issue
To create an issue interactively with prompts use:gh issue create
-a, --assignee login Assign people by their login
-b, --body "string" Add the body of the issue
-l, --label name Add labels by name
-m, --milestone name Add the issue to a milestone by name
-p, --project name Add the issue to projects by name
-t, --title "string" Supply a title.
-w, --web Create an issue in the browser
gh issue status
gh issue close issueNumber or issueURL
gh issue reopen issueNumber or issueURL
If you forget any command or its flags or if you want to consult the documentation quickly then you can always use the “ — help” flag to view the documentation of the command in the terminal itself.