GitHub: CLI tool for repository operations

GitHub has a CLI tool that allows you to perform many of the operations you may currently perform in the web UI.

Among its many uses, the ‘gh‘ utility will allow you to fork repositories, modify issues, merge PR, and create releases all from the console for ease of use or automation.

Below I will describe its installation on Ubuntu and then examples of usage.

Install

From the documentation, install ‘gh’ using apt for Debian/Ubuntu.

# apt signing key
curl -fsSL https://cli.github.com/packages/githubcli-archive-keyring.gpg | sudo dd of=/usr/share/keyrings/githubcli-archive-keyring.gpg

# create apt source
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

# refresh
sudo apt update
# if GPG error about signatures, add new signing key and try again
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-key 23F3D4EA75716059

# install
sudo apt install gh -y

# validate
gh version

Authenticate

You will be prompted to press <ENTER>, which opens a browser to github.com for authentication.

# authenticate with browser (-w)
$ gh auth login --hostname github.com -p https -w

? Authenticate Git with your GitHub credentials? Yes

! First copy your one-time code: 1234-5678
Press Enter to open github.com in your browser... 
✓ Authentication complete.
- gh config set -h github.com git_protocol https
✓ Configured git protocol
✓ Logged in as fabianlee

Create Personal fork

Fork one of my github public projects to your own personal fork (will be deleted in later step).

# fork either with http or ssh
#gh repo fork https://github.com/fabianlee/gke-acm-kustomize-public.git
gh repo fork git@github.com:fabianlee/gke-acm-kustomize-public.git
cd gke-acm-kustomize-public
git remote -v

Load ssh key for authentication

If you want to provide authentication into this git repo for a 3rd party integration, then load the public side of an ssh keypair into the repository settings.

# create public private ssh keypair
git_id=$(git config user.email)
ssh-keygen -t rsa -b 4096 -C "$git_id" -N '' -f testkey

# upload public side of ssh key to repo
gh repo deploy-key add testkey.pub

Rename repo

To prepare for making the visibility of this repo private (not viewable to the public), let’s rename it.

# rename repo
gh repo rename gke-acm-kustomize-private

# rename local dir to match
cd ..
mv gke-acm-kustomize-public gke-acm-kustomize-private
cd gke-acm-kustomize-private

Make visibility private

Now we will make this repository private so that only yourself and those you explicitly assign have visibility.

gh repo edit --visibility private

Delete ssh key

If you want to remove the ssh key for auth loaded earlier.

# delete key from repo
gh repo deploy-key list
key_id=(gh repo deploy-key list | cut -f1)
gh repo deploy-key delete $key_id

Delete repository

You will be prompted with a code and then a browser popup to authenticate this command.

# delete repo, first give auth scope to do deletions
gh auth refresh -h github.com -s delete_repo

# will be prompted for auth
gh repo delete gke-acm-kustomize-private

cd ..
rm -fr gke-acm-kustomize-private

 

REFERENCES

gh command list

gh help reference with all flags

gh install

gh fork

gh deploy-key

gh rename

gh repo delete

techiediaries.com, install gh client on ubuntu

NOTES

If you get upgrade message when running gh

sudo apt install gh --only-upgrade