Adding `gh` to my pull request workflow
In the past, I used hub to create GitHub pull requests (PRs). It was a crucial
tool for my workflow because it allowed me to create PRs easily and quickly from
the command line. But recently, I decided to replace hub
with GitHub’s newer,
official CLI (gh).
At first, I found gh
to be too verbose. It had too many prompts and steps for
things I wanted to do with a single command. But soon enough, I found the
correct commands and flags to fine-tune my workflow. And now I’m loving it.
Here are some of my favorite commands and flags to deal with PRs.
Creating Pull Requests
gh
gives us a simple helper to create PRs:
gh pr create
The basic command makes you walk through prompts to write the title and
description. But typically, by the time I open a PR, I have already squashed my
commits into a single commit with a good title and description. And I want to
use those for the PR. That’s why I like the -f
flag:
-f, --fill Do not prompt for title/body and just use commit info
The —f
flag uses my commit’s title and message. That’s what I want! A single
command to turn my commit into a pull request:
gh pr create -f
Inspecting code changes before opening a PR
Sometimes I like to inspect the changes I’ll submit before opening the PR. I
used to accomplish that with hub compare
, which allowed me to compare my
current branch against main
.
gh
doesn’t have a perfect equivalent, but there’s a very close relative, the
-w
flag:
gh pr create -w
The -w
flag opens the web browser in the state right before opening the PR.
There you can see the comparison of the changes you’ll submit.
Checking the status of a PR
One of the additions I like in gh
is the ability to check the status of a PR’s
continuous integration tasks. I really dislike having to open the browser,
navigate to the correct PR, and see if all the checks are green or if something
failed. That breaks apart my workflow too much.
gh
ships with a handy pr checks
command:
gh pr checks
It also has a nice --watch
flag if we don’t want to keep manually typing that
command every few seconds. With the --watch
flag, gh
will continue checking
the status from the command line every 10 seconds until all checks finish:
gh pr checks —-watch
When the checks finish, the command prints one link for each check, showing us which checks passed and which failed. That makes it easy to open the browser directly to the failed check. And if all checks pass, we can move on to merging the PR.
Merging a PR
With hub
, I couldn’t merge PRs with GitHub’s squash or rebase from the command
line. But gh
has an excellent command that is a big quality-of-life
improvement.
The basic command is this:
gh pr merge
But it has the nice -s
and -r
flags for squashing and rebasing (similar to
what you could do in the browser):
gh pr merge -s
And
gh pr merge -r
Be sure to do that only if your commit message is good enough! Don’t auto-squash if you have a series of commits that need to be consolidated into a good message. As I’ve said before, don’t forget (or skip) the silent step when squashing.
Bonus! Auto squash, merge, and delete!
One last thing I like about the gh pr merge
command is that you can request it
to delete the remote and local branches once it’s done merging code. It saves
me the hassle of having to do that on my own.
Just add the -d
flag!
gh pr merge -s -d