DEV Community

Cover image for How to use Github CLI in 7 commands
thomasvanholder
thomasvanholder

Posted on

How to use Github CLI in 7 commands

  1. Open project in Github
  2. List all issues
  3. Create new issue
  4. List all PRs
  5. Check PR status
  6. Merge PR into main
  7. Switch between branches

Github CLI installed?
Check by running gh version

Or install Github CLI


1. Open project in Github

Open the github homepage of the current project in the browser.

gh browse
Enter fullscreen mode Exit fullscreen mode

Github CLI's response:

Opening github.com/repo/my-repo in your browser.
Enter fullscreen mode Exit fullscreen mode

2. List all issues

Return all outstanding issues in the repository.

gh issue list
Enter fullscreen mode Exit fullscreen mode

Github CLI's response:

Showing 3 of 3 open issues in repo/my-repo

#178  bug when downloading pdf            about 3 minutes ago
#166  refactor stripe api call            about 2 days ago
#126  depreciate pacakge                  20 days ago
Enter fullscreen mode Exit fullscreen mode

3. Create new issue

Did you find a new bug?
Add something as a to-do for later on Github Issues. The CLI will prompt you a few questions.

gh issue create
Enter fullscreen mode Exit fullscreen mode

Github CLI's response:

Creating issue in repo/my-repo

? Title bug when downloading pdf
? Body <Received>
? What's next? Submit
https://github.com/repo/my-repo/issues/178
Enter fullscreen mode Exit fullscreen mode

4. List all PRs

Retrieve an overview of all PR in the repository.

gh pr list
Enter fullscreen mode Exit fullscreen mode

Github CLI's response:

Showing 3 of 3 open pull request in repo/my-repo

#170  feat/stripe-checkout
#142  feat/vitally-api
#139  bug/profile-responsiveness
Enter fullscreen mode Exit fullscreen mode

5. Check PR status

To find out if the PR passed the automated testing suite.

gh pr status
Enter fullscreen mode Exit fullscreen mode

Github CLI's response:

Relevant pull requests in repo/my-repo

Current branch
  #177  new-feature [new-feature]
  ✓ Checks passing
Enter fullscreen mode Exit fullscreen mode

6. Merge PR into main

A useful shortcut to easily delete a branch from both Github and your local enviroment.

2 options:

  • From the local main branch

Merge a PR into the main branch by specifying the PR number.

gh pr merge 177
Enter fullscreen mode Exit fullscreen mode
  • From the PR branch

No need to specific the PR number.

gh pr merge
Enter fullscreen mode Exit fullscreen mode

Github CLI's response:

? What merge method would you like to use? Create a merge commit
? Delete the branch locally and on GitHub? Yes
? What's next? Submit
✓ Merged pull request #177 (new-feature)
✓ Deleted branch new-feature
Enter fullscreen mode Exit fullscreen mode

7. Switch between branches

This CLI command is based upon a popular extension.

Install the extension

gh extension install mislav/gh-branch
Enter fullscreen mode Exit fullscreen mode

Run the extension.

gh branch
Enter fullscreen mode Exit fullscreen mode

Move across branches with up and down arrow. Hit enter to select the branch.

Github CLI's response:

  feat/stripe-checkout             6 weeks ago
  feat/postmark-integration        2 weeks ago
> new-feature                      25 hours ago
  bug/profile-responsiveness       2 hours ago
  4/4
Enter fullscreen mode Exit fullscreen mode

Oldest comments (2)

Collapse
 
townofdon profile image
Don Juan Javier

I don't know why, I always prefer using the CLI for everything except merging PRs - I always use the GUI for that. Maybe pressing the button makes it feel more official somehow... one last chance to check things over.

This command reference sheet is very helpful, thanks!

Collapse
 
thomasvanholder profile image
thomasvanholder

That green merging button has a long legacy. It is definitely linked to making big changes work (or fail when something was missed 👀).

When a CI testing suite is set-up it gives more confidence to merge by cli. It's one my favorite timesavers as it automatically deletes the branch on github and local. A few second here and there, over again.