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 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
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:
authbrowseissueprreleaserepocodespacegist
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
You can also fork existing repositories to your account easily from the command line.
gh repo fork cli/cli
To view the description and README of a project hosted on GitHub use the gh repo view command.
gh repo view facebook/react
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
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
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
A short version:
gh pr create --title "PR title" --body "PR body"
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
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
Additional gh pr commands
: show the status of relevant pull requests.
: add a review to a pull request.
: reopen a pull request.
By now, there isn't a way to revert PR from the GitHub CLI yet :(
Issue Command
: displays the title, body, and other information about an issue.
: close an issue.
for new features and detailed info on using the existing commands.
SOCIAL SHARE CARD GENERATOR