Getting started with Github CLI
GitHub (GH from now) just released their first stable version of their CLI , the tool for managing and interacting with your GH repositories from your terminal. No more back & forth between the terminal & web browser for creating PRs, forking repositories, and whatnot. And if you or your company uses GH as their issue tracking tool, even better!
I started to play with it as I find it useful to have an uninterrupted workflow entirely in my terminal. Usually, I have to fork repositories, create feature branches, do my stuff, commit-push, and then create PR to the original repository. Now I want to share my experience with you.
Let's get hands on! Let's start by trying to create a repository in our GH account. We do so with the command
ocalderon@pop-os:~$ gh repo create main-repo
However, since we haven't authenticated in our CLI, we will get a message indicating to do so.
ocalderon@pop-os:~$ gh repo create main-repo Welcome to GitHub CLI! To authenticate, please run `gh auth login`. You can also set the GITHUB_TOKEN environment variable, if preferred.
Running the command
gh auth login will get us through a simple process to authenticate via web browser or using a generated token from GH webpage.
ocalderon@pop-os:~$ gh auth login ? What account do you want to log into? GitHub.com - Logging into github.com ? How would you like to authenticate? Login with a web browser ! First copy your one-time code: 0123-ABCD - Press Enter to open github.com in your browser...
Going with the web option will generate a unique code that we have to paste in the GH webpage that is opened by the command.
Then we review the permissions to give to the application, and we are in!
✓ Authentication complete. Press Enter to continue... ? Choose default git protocol SSH - gh config set -h github.com git_protocol ssh ✓ Configured git protocol ✓ Logged in as ocalde
Now that we authenticated through
gh, we can try again to create our repository. It will ask us some questions, like repository visibility, or if we want to clone it in our current repository.
ocalderon@pop-os:~$ gh repo create main-repo ? Visibility Private ? This will create 'main-repo' in your current directory. Continue? Yes ✓ Created repository ocalde/main-repo on GitHub ? Create a local project directory for ocalde/main-repo? Yes Initialized empty Git repository in /home/ocalderon/main-repo/.git/ ✓ Initialized repository in './main-repo/'
And that's it. You can check in your GH account to see that the repository has been created successfully.
We start by trying to fork the repository, using
This happens because we haven't configured any SSH keys in our GH account. You can follow these steps to generate your SSH key , and configure it in your account.
Once we are done with that, we can try again to fork the repo (we will be asked for the passphrase which we used to generate our SSH key):
Repository forked and cloned! We then can create a feature branch, do some changes, commit & push them, and once we are ready, we can create a PR, with
Like previous commands, we are asked for information like PR title, body (we can specify these when typing the
gh pr command). Also, I used
-H to specify the base & head branches, although I think it can ask for it as well if it isn't clear.
If everything goes right, the PR is created automatically, and the command output gives you the URL to check it.
And this is just a sample of what you can do using GH CLI. As I mentioned earlier, you can also handle the workflow of working with issues inside GH, and a lot of other things. You can check cli.github.com/manual for a complete reference of all the available commands.