DEV Community

Cover image for GitHub CLI: gives wings to terminal
Vishnu Prasaath
Vishnu Prasaath

Posted on • Originally published at blog.vishnuprasaath.dev on

GitHub CLI: gives wings to terminal

When working with git, youre obviously aware that you need to switch to the web browser to perform various actions on the GitHub repo - Pull Request, Issues, Review... The GitHub CLI tool gives wings to the cmd-line, so you can execute many of these actions without leaving the cmd-line.

Setup

To get started, visit the installation page and find instructions on how to install GitHub CLI for your preferred OS. The easy-peasy way for Windows users is to use scoop package manager.

Below are snapshots of install instructions for each supported platform:

  • Windows:

  • macOS:

  • Debian/Ubuntu Linux:


Authenticate GitHub CLI

Authenticate CLI to access your GitHub account.

gh auth login

Enter fullscreen mode Exit fullscreen mode

img1

Select Login with a Web browser

$ gh auth login
? What account do you want to log into? GitHub.com
? What is your preferred protocol for Git operations? HTTPS
? Authenticate Git with your GitHub credentials? Yes
? How would you like to authenticate GitHub CLI? Login with a web browser

! First copy your one-time code: 1DD9-7BD3
Press Enter to open github.com in your browser... 
 Authentication complete.
- gh config set -h github.com git_protocol https
 Configured git protocol
 Logged in as pvishnuprasaath

Enter fullscreen mode Exit fullscreen mode

img2


GitHub CLI Command Struct

gh command is structured like a tree for grabbing easily. There are basically two levels of commands. The first level consists of the below commands:

  • auth

  • browse

  • issue

  • pr

  • release

  • repo

  • codespace

  • gist

Each command has a second level of command where you specify the operation to perform such as gh pr create or gh repo view.

Let's dive deep into the most commonly used commands.


GitHub Repo Command

Cloning a repo with the gh command is easier than using the git command. You no longer need to type or copy-paste the long Git URL to clone

gh repo clone OWNER/REPO
gh repo clone mui/material-ui

Enter fullscreen mode Exit fullscreen mode

You can also fork existing repositories to your account easily from the command line.

gh repo fork cli/cli

Enter fullscreen mode Exit fullscreen mode

To view the description and README of a project hosted on GitHub use the gh repo view command.

gh repo view facebook/react

Enter fullscreen mode Exit fullscreen mode

Let's create a new GitHub repository from the command line. First, we need to create a new project.

$ npx create-next-app sample-app
$ cd sample-app

Enter fullscreen mode Exit fullscreen mode

To create a repo from the command line, just run the following:

$ gh repo create --public
 Created repository pvishnuprasaath/sample-app on GitHub
 Added remote https://github.com/pvishnuprasaath/sample-app.git

# Push your project code to your new remote repository
$ git push -u origin main

Enter fullscreen mode Exit fullscreen mode

Pull Request Command

If you know classic git flow , creating PR is quite an effort. GitHub CLI makes it easy to create PR directly from your terminal. After a git commit, you can execute gh pr create. It will prompt a couple of inputs. Post that, the PR link is displayed in the terminal.

Heres the full output:

$ gh pr create
Creating pull request for feature-1 into master in pvishnuprasaath/sample-app
? Title Added new feature
? Body Implemented and testted new feature for ABC
? What is next? Submit
Enumerating objects: 6, done.
Counting objects: 100% (6/6), done.
Delta compression using up to 8 threads
Compressing objects: 100% (4/4), done.
Writing objects: 100% (4/4), 328 bytes | 328.00 KiB/s, done.
Total 4 (delta 2), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (2/2), completed with 2 local objects.
remote:
remote: Create a pull request for 'feature-1' on GitHub by visiting:
remote: https://github.com/pvishnuprasaath/sample-app/pull/new/feature-1
remote:
To https://github.com/pvishnuprasaath/sample-app.git
* [new branch] HEAD -> feature-1
Branch 'feature-1' set up to track
remote branch 'feature-1' from 'origin'.
https://github.com/pvishnuprasaath/sample-app/pull/3

Enter fullscreen mode Exit fullscreen mode

A short version:

gh pr create --title "PR title" --body "PR body"

Enter fullscreen mode Exit fullscreen mode

To view all the pull requests in the current repo, run gh pr list

$ gh pr list
Showing 1 of 1 pull request in pvishnuprasaath/sample-app
#3 Added new feature feature-1

Enter fullscreen mode Exit fullscreen mode

Use the command gh pr checkout <number> to checkout a PR. This command will pull the remote feature branch and switch to it. gh pr diff will show the delta for the current PR.

Lets take a look at the gh pr merge command. As youre probably aware, GitHub checks for vulnerabilities in your code and provides solutions via bump pull requests. Heres an example:

Merging multiple PRs one by one is a tiring process. gh pr merge makes it easy to merge a particular PR directly from the terminal.

$ gh pr merge 4
? What merge method would you like to use? Create a merge commit
remote: Counting objects: 100% (1/1), done.
remote: Counting objects: 100% (1/1), done.
remote: Total 1 (delta 0), reused 0 (delta 0), pack-reused 0        
Unpacking objects: 100% (1/1), 624 bytes | 89.00 KiB/s, done.       
From https://github.com/pvishnuprasaath/sample-app   
 * branch main -> FETCH_HEAD
   09f8c52..7a8a3f2 main -> origin/main
Updating 09f8c52..7a8a3f2
Fast-forward
 src/data/data.ts | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
 Deleted branch FixLink and switched to branch main

Enter fullscreen mode Exit fullscreen mode

Additional gh pr commands

By now, there isn't a way to revert PR from the GitHub CLI yet :(


Issue Command

Issues are used to keep track of bugs, tasks and feature requests associated with a GitHub project repo. The command gh issue create is used to create a new issue via the terminal:

$ gh issue create
Creating issue in pvishnuprasaath/sample-app
? Title Fix Docker skill link

? Title Fix Docker skill link
? Body <Received>
? What's next? Submit
https://github.com/pvishnuprasaath/sample-app/issues/6

Enter fullscreen mode Exit fullscreen mode

gh issue list command lists all issues in the current repo.

The rest of gh issue commands are quite similar to gh pr commands. Below is a quick summary:


GitHub CLI Cheat Sheet

Creating, deleting and configuring repo

gh repo create

gh repo create <name_of_repo> --public

gh repo create my-project --private --source=. --remote=upstream

gh repo edit --visibility <visibility-string>

gh repo sync

gh repo create --disable-issues=true --public

gh repo list

gh repo delete <name_of_repo>

gh repo clone <name_of_repo>

gh repo fork <name_of_repo>

Enter fullscreen mode Exit fullscreen mode

To easily save a piece of code online use Gist:

Creating a gist

touch index.js // after create content for it

gh gist create index.js --public

gh gist create -

gh gist edit <gist_id> 

gh gist list --public 

gh gist list --secret

Enter fullscreen mode Exit fullscreen mode

Handling issues

gh issue create

gh issue list

gh issue status

git issue close <#number_issue>

gh list -A "<name_issue>"

Enter fullscreen mode Exit fullscreen mode

Handling Pull requests

gh pr create 

gh pr checkout <name>

gh pr diff <#number_of_pr>

gh review -c -b "nice work"

gh pr close <#number_of_pr> -d

gh pr reopen <#number_of_pr>

gh pr status

Enter fullscreen mode Exit fullscreen mode

gh for sure saves up time in devflow. Gives additional hands to your terminal for managing the repository. Refer to the official docs for new features and detailed info on using the existing commands.

Top comments (0)