GitHub CLI
GitHub has a CLI available at https://cli.github.com. If you use GitHub a fair amount it quickly becomes useful not just to avoid leaving the programming environment, but also to automate operations. Here are some examples:
Clone to local computer all repositories belonging to a user
This is useful for a new backup of all repositories, or more frequently to populate a new VM with all the repositories that may be potentially needed, e.g., if a disconnect from the internet is expected.
gh repo list myusername | cut -f1 | xargs -I {} gh repo clone {}
Search github
This can be useful to keep an eye on certain topics frequently. For example to search for all repositories with “memray” (an opensource profiler from Bloomberg) crated since beginning of 2022, you can use:
gh search repos memray "created:>2022-01-01"
Find which local ssh-key is registred with the github account
When using git+ssh the first key offered needs to match the right github account, so if you have multiple keys and accounts this can lead to some confusion. The following snippets will match the local keys with those that are uploaded to current account:
comm -12 <(gh ssh-key list | cut -f2 | sort) <(cat ~/.ssh/*.pub | cut -f1-2 -d ' ' | sort)
Note the use of process substitution <( command )
syntax to
avoid use of temporary files. Alternative more information can be
presented using the join command:
join -j2 <(gh ssh-key list | cut -f2 | sort) <(cat ~/.ssh/*.pub | sort)