Git and libgit2

GitHub CLI

Published 2024-05-20.
Time to read: 2 minutes.

This page is part of the git collection.

The command-line interface provided by the GitHub CLI provides a high-productivity means for interacting with GitHub.

Creating GitHub Repositories

The gh repo create subcommand can be used interactively, or arguments can be supplied. This article describes non-interactive usage.

The following two aliases create a GitHub repository from a local repository. The gh_new_public alias creates a public GitHub reposiory, and the gh_new_private alias creates a private GitHub repository. Both aliases push local commits to the remote repository after creation. The new GitHub repository will have the same name as the directory containing the local Git repository.

~/.bash_aliases
alias gh_new_private='gh repo create --private --source=. --remote=origin --push'
alias gh_new_public='gh repo create --public --source=. --remote=origin --push'

Here is an example of how to use the gh_new_public alias to create a new public GitHub repository called my_new_project.

Shell
$ git init my_new_project
Initialized empty Git repository in /mnt/c/work/git/my_new_project/.git/ 
$ cd my_new_project
$ cat > README.md << EOF # My New Project All-singing, all-dancing fiddly bits! EOF
$ git add -A
$ git commit -m First
$ gh_new_public ✓ Created repository mslinn/my_new_project on GitHub ✓ Added remote git@github.com:mslinn/my_new_project.git Enumerating objects: 3, done. Counting objects: 100% (3/3), done. Delta compression using up to 12 threads Compressing objects: 100% (2/2), done. Writing objects: 100% (3/3), 260 bytes | 9.00 KiB/s, done. Total 3 (delta 0), reused 0 (delta 0), pack-reused 0 To github.com:mslinn/my_new_project.git * [new branch] HEAD -> master branch 'master' set up to track 'origin/master'. ✓ Pushed commits to git@github.com:mslinn/my_new_project.git

Forking A GitHub Repository

It is common practice to fork someone else's repository, clone the fork to a local directory, then create a remote called upstream that points at the origin repository. That allows you to update the local clone of the fork to the latest work from the original repository by typing:

Shell
$ git pull upstream

The GitHub CLI provides a nice way of doing this.

gh repo fork

gh repo fork [<repository>] [-- <gitflags>...] [flags]

Create a fork of a repository.

With no argument, creates a fork of the current repository. Otherwise, forks the specified repository.

By default, the new fork is set to be your origin remote and any existing origin remote is renamed to upstream. To alter this behavior, you can set a name for the new fork's remote with ‑‑remote-name.

The upstream remote will be set as the default remote repository.

These are the commands I used to fork the JUCE repository on GitHub:

Shell
$ cd $work # The directory tree where I store my work
$ git clone https://github.com/juce-framework/JUCE.git
$ cd JUCE
$ gh repo fork ✓ Created fork mslinn/JUCE ? Would you like to add a remote for the fork? Yes ✓ Added remote origin

In the future, when it is time to update the version of JUCE used by my application to the current HEAD, I will just type:

Shell
$ git pull upstream
remote: Enumerating objects: 141, done.
remote: Counting objects: 100% (141/141), done.
remote: Compressing objects: 100% (58/58), done.
remote: Total 141 (delta 80), reused 138 (delta 80), pack-reused 0
Receiving objects: 100% (141/141), 498.14 KiB | 2.59 MiB/s, done.
Resolving deltas: 100% (80/80), completed with 32 local objects.
From https://github.com/juce-framework/JUCE
    eac95d590..bbc63ba15  develop    -> upstream/develop
    7c2a5fc75..b9a1bfbc2  juce8      -> upstream/juce8
Already up to date. 
* indicates a required field.

Please select the following to receive Mike Slinn’s newsletter:

You can unsubscribe at any time by clicking the link in the footer of emails.

Mike Slinn uses Mailchimp as his marketing platform. By clicking below to subscribe, you acknowledge that your information will be transferred to Mailchimp for processing. Learn more about Mailchimp’s privacy practices.