gh – GitHub Command Line Tool

211123-1126
Summary
This is a short intro for the gh -the GitHub Command Line Tool. Prior to the gh introduction, most of the job, working with terminal and trying to automate somehow the interaction with GitHub repositories, required the using of other CLI tools like curl. This was not such a handy approach, especially for DevOps. But now, we can rely on the gh tool that simplifies quite a lot, the automation of operations needed to deal with GitHub repos.
Intro
The official GitHub CLI (Command Line Interface) has been available since September 2020. https://cli.github.com/

Now, it has reached to its version 2.2.0, and it is available for all major OS.https://github.com/cli/cli/releases

Installation
On Mac via brew
Just use the command:
brew install gh

Check the version installed:
➜ TSProjects ➜ TSProjects gh --version gh version 2.2.0 (2021-10-25) https://github.com/cli/cli/releases/tag/v2.2.0 ➜ TSProjects
On Debian/Ubuntu
panosku@panosku-MS-7B18:~$ sudo curl -fsSL https://cli.github.com/packages/githubcli-archive-keyring.gpg | sudo dd of=/usr/share/keyrings/githubcli-archive-keyring.gpg [sudo] password for panosku: 3+1 records in 3+1 records out 1795 bytes (1.8 kB, 1.8 KiB) copied, 0.298376 s, 6.0 kB/s panosku@panosku-MS-7B18:~$ echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" | sudo tee /etc/apt/sources.list.d/github-cli.list > /dev/null panosku@panosku-MS-7B18:~$ sudo apt update Hit:1 http://dl.google.com/linux/chrome/deb stable InRelease Hit:2 http://packages.microsoft.com/repos/code stable InRelease Hit:3 http://security.ubuntu.com/ubuntu focal-security InRelease Hit:4 http://gr.archive.ubuntu.com/ubuntu focal InRelease Hit:5 https://download.docker.com/linux/ubuntu focal InRelease Hit:6 https://linux.teamviewer.com/deb stable InRelease Hit:7 http://gr.archive.ubuntu.com/ubuntu focal-updates InRelease Get:8 https://cli.github.com/packages stable InRelease [3743 B] Hit:9 http://gr.archive.ubuntu.com/ubuntu focal-backports InRelease Get:10 https://cli.github.com/packages stable/main amd64 Packages [337 B] Fetched 4080 B in 2s (2394 B/s) Reading package lists... Done Building dependency tree Reading state information... Done 68 packages can be upgraded. Run 'apt list --upgradable' to see them. panosku@panosku-MS-7B18:~$ sudo apt install gh Reading package lists... Done Building dependency tree Reading state information... Done The following packages were automatically installed and are no longer required: libllvm9 libstartup-notification0 qtgstreamer-plugins-qt5 shim Use 'sudo apt autoremove' to remove them. The following NEW packages will be installed: gh 0 upgraded, 1 newly installed, 0 to remove and 68 not upgraded. Need to get 7300 kB of archives. After this operation, 30.3 MB of additional disk space will be used. Get:1 https://cli.github.com/packages stable/main amd64 gh amd64 2.2.0 [7300 kB] Fetched 7300 kB in 1s (5240 kB/s) perl: warning: Setting locale failed. perl: warning: Please check that your locale settings: LANGUAGE = "en_US:el", LC_ALL = (unset), LC_TIME = "el_GR.UTF-8", LC_MONETARY = "el_GR.UTF-8", LC_ADDRESS = "el_GR.UTF-8", LC_TELEPHONE = "el_GR.UTF-8", LC_NAME = "el_GR.UTF-8", LC_MEASUREMENT = "el_GR.UTF-8", LC_IDENTIFICATION = "el_GR.UTF-8", LC_NUMERIC = "el_GR.UTF-8", LC_PAPER = "el_GR.UTF-8", LANG = "en_001.UTF-8" are supported and installed on your system. perl: warning: Falling back to the standard locale ("C"). locale: Cannot set LC_CTYPE to default locale: No such file or directory locale: Cannot set LC_MESSAGES to default locale: No such file or directory locale: Cannot set LC_ALL to default locale: No such file or directory Selecting previously unselected package gh. (Reading database ... 393782 files and directories currently installed.) Preparing to unpack .../archives/gh_2.2.0_amd64.deb ... Unpacking gh (2.2.0) ... Setting up gh (2.2.0) ... Processing triggers for man-db (2.9.1-1) ... panosku@panosku-MS-7B18:~$
Usage
Authentication and Login
Use a GitHub Personal Access Token (PAT). Go to https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/creating-a-personal-access-token for instruction on how to generate a Personal Access Token.
Pay attention to set up your PAT correctly with the minimum required scopes:
‘read:org’, ‘repo’, ‘workflow’
and probably:
‘gist’, ‘write:packages’
Otherwise, you will face an error like:
➜ TSProjects gh auth login --with-token < ~/ghtoken1.txt error validating token: missing required scope 'read:org' ➜ TSProjects
For example:

Create a PAT, and store it safely in a file. For instance:
➜ ~ pwd /Users/zp ➜ ~ touch ghtoken1.txt ➜ ~ echo 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 >> ghtoken1.txt ➜ ~ cat ghtoken1.txt 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 ➜ ~
Authenticate against github.com by reading your token from a file:
➜ TSProjects gh auth login --with-token < ~/ghtoken1.txt ➜ TSProjects
Create a new Public repo
Jump into your project root folder, e.g. ‘tsbrowser’.
First, you have to create your .gitignore file (If you haven’t done so yet), e.g.:
➜ tsbrowser echo "node_modules\n.DS_Store\n" > .gitignore && cat .gitignore node_modules .DS_Store ➜ tsbrowser
Then, initialize the local repo, add all to stash, and commit (If you haven’t done so yet):
➜ tsbrowser git init Initialized empty Git repository in /Users/zafeiropoulospanos/TSProjects/tsbrowser/.git/ ➜ tsbrowser git:(master) ✗ git add . ➜ tsbrowser git:(master) ✗ git commit -m "The 1st Commit" [master (root-commit) 72f9dff] The 1st Commit 8 files changed, 233 insertions(+) create mode 100644 .gitignore create mode 100644 index.html create mode 100644 package-lock.json create mode 100644 package.json create mode 100644 src/app/modules/myfunctions.ts create mode 100644 src/index.ts create mode 100644 style.css create mode 100644 tsconfig.json ➜ tsbrowser git:(master)
Make your main branch (If you haven’t done so yet):
➜ tsbrowser git:(master) ➜ tsbrowser git:(master) git branch -M main ➜ tsbrowser git:(main) ➜ tsbrowser git:(main)
Now, is the time to use the gh to create a new repo on GitHub:
➜ tsbrowser git:(main) gh repo create 'tsbrowser' --description 'Typescript: Start a browser-based project (System.js)' --public --confirm ✓ Created repository zzpzaf/tsbrowser on GitHub ✓ Added remote https://github.com/zzpzaf/tsbrowser.git ➜ tsbrowser git:(main)
You can check it also via your browser:

Which of course is empty till now:

The next step is to add as remote ‘origin’ the remote repo we’ve just created:
➜ tsbrowser git:(main) git remote add origin https://github.com/zzpzaf/tsbrowser.git
Push our default (“main”) branch on GitHub
Finally, you are ready to push your local branch to remote repo, using the git push command:
➜ tsbrowser git:(main) git push origin main Counting objects: 13, done. Delta compression using up to 4 threads. Compressing objects: 100% (10/10), done. Writing objects: 100% (13/13), 5.44 KiB | 1.81 MiB/s, done. Total 13 (delta 0), reused 0 (delta 0) To https://github.com/zzpzaf/tsbrowser.git * [new branch] main -> main ➜ tsbrowser git:(main)
After that, you can check it again via your browser. Now t’s OK:

Clone a repo from GitHub
Now, anyone can go to a (public) repo and clone it using the gh:

panosku@panosku-MS-7B18:~$ gh repo clone zzpzaf/tsbrowser Cloning into 'tsbrowser'... remote: Enumerating objects: 13, done. remote: Counting objects: 100% (13/13), done. remote: Compressing objects: 100% (10/10), done. remote: Total 13 (delta 0), reused 13 (delta 0), pack-reused 0 Unpacking objects: 100% (13/13), 5.42 KiB | 925.00 KiB/s, done. panosku@panosku-MS-7B18:~$ ls -al
Want more? Read here the gh manual!
That’s it!
Enjoy, and stay tuned.
Thanx for reading!