DEV Community

Calvin Nguyen
Calvin Nguyen

Posted on • Edited on

I tried Github CLIs...and I never open browser again!

Update: I added #discuss, so please if anyone of you knows new features of Github CLIs, feel free to comment. Below, I found more cool things I could do with gh.

https://cdn-images-1.medium.com/max/1600/0*_Gp8d1a15_03NZLb

Inspiration

  • GitHub tools are all great, but sometimes, I get exhausted as I’m coding and have to switch from my terminal/text editor to do many clicks in the browser, just to create or view a PR.

  • Luckily, on March 6th, 2020, GitHub released a new cool GitHub CLI to create PR/ Issue in your local terminal.

In this post, I will go over 1 main command that I usually daily and highly recommend: gh pr <command>.


Installment

macOS

  1. Easy Install With brew: $ brew install github/gh/gh
  2. Upgrade: brew update && brew upgrade gh

Windows

Install gh via Scoop:

scoop bucket add github-gh https://github.com/cli/scoop-gh.git
scoop install gh

Linux

For Linux and other platforms, please refer to this GitHub page for different installment

Normal flow vs. new Github CLI

Old Process of creating a PR:

  1. (feat/login)$ git add .

  2. (feat/login)$ git commit -m "init skeleton code for login"

  3. (feat/login)$ git push origin feat/login

  4. Open Github on browser

  5. Open Github project

  6. Click Create a PR

  7. Fill the title and body

Now, let's see how many steps when we use gh pr create:

  1. (feat/login)$ git add .

  2. (feat/login)$ git commit -m "init skeleton code for login"

3.

(feat/login)$ gh pr create
Enumerating objects: 9, done.
Counting objects: 100% (9/9), done.
Delta compression using up to 4 threads
Compressing objects: 100% (5/5), done.
Writing objects: 100% (5/5), 444 bytes | 444.00 KiB/s, done.
Total 5 (delta 4), reused 0 (delta 0)
remote: Resolving deltas: 100% (4/4), completed with 4 local objects.
To github.com:calvinqc/trivin.git
   1c179cb..7953ecc  HEAD -> gh-testing
Branch 'feat/login' set up to track remote branch 'feat/login' from 'origin'.

Creating pull request for feat/login into master in calvinqc/trivin
? Title [WIP] Login API
? Body <Received>
? What's next? Submit
https://github.com/calvinqc/trivin/pull/1

Options

Source from Github CLIs:

  -a, --assignee login   Assign people by their login
  -B, --base string      The branch into which you want your code merged
  -b, --body string      Supply a body. Will prompt for one otherwise.
  -d, --draft            Mark pull request as a draft
  -f, --fill             Do not prompt for title/body and just use commit info
  -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
  -r, --reviewer login   Request reviews from people by their login
  -t, --title string     Supply a title. Will prompt for one otherwise.
  -w, --web              Open the web browser to create a pull request

Easy 1 line command:

$ gh pr create -t "[WIP] Login API" -b "Init skeleton code for Login API" -r "calvinqc" -l "feat" -p "Roadmap" -B dev

$

If there’s one thing that I love about gh pr create, it is that it will push + create a PR at the same time.

List all PRs

$ gh pr list
Pull requests for calvinqc/trivin
#2  [WIP] Register API  feat/register
#1  [WIP] Login API     feat/login

View a PR

$ gh pr view 1
Opening https://github.com/calvinqc/trivin/pull/1 in your browser

Get status of all PRs

$ gh pr status
Relevant pull requests in calvinqc/trivin
Current branch
  #1  [WIP] Login API     feat/login
   - Checks passing
Created by you
  #2  [WIP] Register API  feat/register
   - Checks passing
  #1  [WIP] Login API     feat/login
   - Checks passing
Requesting a code review from you
  You have no pull requests to review

Checkout a PR

This is very similar to a regular branch checkout, but I don't have to checkout and then pull the code from that branch

$ gh pr checkout 1

Let's connect

Top comments (18)

Collapse
 
juanfrank77 profile image
Juan F Gonzalez

I was actually trying out the Github CLI today from my Ubuntu distro and it looked pretty cool to me. It gives the relevant info for issues and also PR's on a repo.

I think I can get used to it more than having to go to the browser. Also, I didn't know that gh pr create did push AND created the pr on GH, that's sooo cool!

Collapse
 
calvinqc profile image
Calvin Nguyen

Yep, that's why I start to use more gh pr create today, doing extra steps on the browser is just too tired

Collapse
 
jwp profile image
John Peters

We don't store our source on github, but.... I do only use command line git commands. I have an advantage doing that over GUI based Git command tools because (as painful as it can get sometimes) I'm forced to learn how Git things. As I move to my opensource projects I will use this article to help me get started, Thanks Calvin.

Collapse
 
calvinqc profile image
Calvin Nguyen

Sure @john , I'm really glad that could help provide great information for people. :). I know sometimes it's really painful with git, but the more I use and make mistake, the more confident I feel about git. so yeah, let's keep on practicing!

Collapse
 
vivinh0 profile image
Viviño

I did't know about this CLI. Very interesting tool, so thanks.

BTW to Linux users out there, you can install brew and then this CLI following macOS commands.

Collapse
 
calvinqc profile image
Calvin Nguyen

Thanks for providing

Collapse
 
vivinh0 profile image
Viviño

Thanks to you for the post.

Collapse
 
kenystev profile image
Kevin J. Estevez

It's awesome, I like it but I'd like to have the option to add a reviewer as well 😅

Collapse
 
adriansamuel profile image
Adrian Samuel

It's already here:

gh pr create --reviewer monalisa,hubot 

Taken from: cli.github.com/manual/gh_pr_create

Collapse
 
kenystev profile image
Kevin J. Estevez • Edited

Wow that should be new because I was looking for it in the documentation of the first release (which I used) and couldn't find it 😯 thanks Adrian

Thread Thread
 
kenystev profile image
Kevin J. Estevez

yeah definitely it's new, I was still on 0.7.0 version and now just updated to 0.11.1

Collapse
 
calvinqc profile image
Calvin Nguyen

Wow, I think it's new, I tried the beta version, and love to do more with the PR with the CLIs (Compare with other branch instead of the default branch)

Collapse
 
thinkverse profile image
Kim Hallberg • Edited

Yeah, that tool is awesome isn't it? 🤘
For personal projects, I usually just add the --fill flag to pre-fill the title and description with the commits.

Collapse
 
calvinqc profile image
Calvin Nguyen

Nice, thanks for the FYI, learn sth new today! Will use it for my personal project

Collapse
 
moopet profile image
Ben Sinclair

Step 3 in your "normal flow" example has an error in it - you don't want to push your local branch to master.

Collapse
 
calvinqc profile image
Calvin Nguyen

lol, dang, I barely use git push these days haha. Thanks for reminding!

Collapse
 
louislow profile image
Louis Low

I start using gh today! It improves my productivity at a significant level.

Collapse
 
khuongduybui profile image
Duy K. Bui

If you use VSCode, try the official GitHub Pull Requests extension. I have always used it in place of opening the browser.