Github CLI
At my current customer we use Github a lot.
Everything requires a review via a Pull Request.
It is sometimes tedious to switch from your IDE or terminal to the browser while creating a PR.
Github has a command line utility called gh
which you can use to automatically create a PR from the command line.
It has lots of extra stuff as well, such as downloading releases, viewing issues, creating gists, cloning repo’s and code completion.
Start by following the installation steps in https://github.com/cli/cli to install the gh binary. There are packages for all operating systems.
Once installed you need to login to your github account once.
$> gh auth login
Code completion is available for the shells bash, zsh, fish and powershell. I use bash and enable it by putting the following line in my .bashrc
eval "$(gh completion -s bash)"
The feature I save most time with is gh pr create
when I need to fix a small issue and create a PR.
$> gh pr create --help
Create a pull request on GitHub.
When the current branch isn't fully pushed to a git remote, a prompt will ask where to push the branch and offer an option to fork the base repository.
Use '--head' to explicitly skip any forking or pushing behavior.
A prompt will also ask for the title and the body of the pull request. Use '--title' and '--body' to skip this, or use '--fill' to autofill these values from git commits.
USAGE
gh pr create [flags]
FLAGS
-a, --assignee login Assign people by their login
-B, --base branch The branch into which you want your code merged
-b, --body string Body for the pull request
-d, --draft Mark pull request as a draft
-f, --fill Do not prompt for title/body and just 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
-r, --reviewer login Request reviews from people by their login
-t, --title string Title for the pull request
-w, --web Open the web browser to create a pull request
INHERITED FLAGS
--help Show help for command
-R, --repo OWNER/REPO Select another repository using the OWNER/REPO format
EXAMPLES
$ gh pr create --title "The bug is fixed" --body "Everything works again"
$ gh pr create --reviewer monalisa,hubot
$ gh pr create --project "Roadmap"
$ gh pr create --base develop --head monalisa:feature
At this point, I created a local branch BUG-332-fix-typo in which I fixed the bug. I can then easily create the PR in Github.
$> git status
On branch BUG-332-fix-typo
nothing to commit, working tree clean
$> gh pr create -f
? Where should we push the 'BUG-332-fix-typo' branch? [Use arrows to move, type to filter]
> toefel18/my-repository
Create a fork of toefel18/my-repository
Skip pushing the branch
Cancel
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 8 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 282 bytes | 282.00 KiB/s, done.
Total 3 (delta 2), reused 0 (delta 0)
remote: Resolving deltas: 100% (2/2), completed with 2 local objects.
remote:
remote: Create a pull request for 'BUG-332-fix-typo' on GitHub by visiting:
remote: https://github.com/toefel18/my-repository/pull/new/BUG-332-fix-typo
remote:
To github.com:toefel18/my-repository.git
* [new branch] HEAD -> BUG-332-fix-typo
Branch 'BUG-332-fix-typo' set up to track remote branch 'BUG-332-fix-typo' from 'origin'.
https://github.com/toefel18/my-repository/pull/238
It saves me some time and I use it daily. Hopefully it helps you as well.