This tutorial is for GNU/Linux Only. The Windows version is here.
First, we need to make sure that we have git
, gnupg
and github-cli
installed.
[alialmasi@parch ~]$ git --version
git version 2.42.0
[alialmasi@parch ~]$ gpg --version
gpg (GnuPG) 2.2.41
libgcrypt 1.10.2-unknown
[alialmasi@parch ~]$ gh version
gh version 2.35.0 (2023-09-19)
https://github.com/cli/cli/releases/tag/v2.35.0
In most GNU/Linux distros, these are pre-installed (except for github-cli
.) If you didn’t have these installed, you can use your distro’s package manager to install them.
You can install github-cli
with any GNU/Linux distro you have.
Note: we’ll use
gnupg
to generate a GPG key pair. we’ll also usegithub-cli
to authenticate ourselves to GitHub (logging into our account with our cli).
git
If you’re new to Git, you probably have to initialize it first. (It’s just a little “Name/Email” configuration, don’t worry.) Open a terminal and do these:
git config --global user.name "YOUR NAME"
Tip: You can confirm that you have set the username correctly by using
git config --global user.name
git config --global user.email "YOUR@EMAIL.COM"
Tip: You can confirm that you have set the email correctly by using
git config --global user.email
To push a local repository to GitHub, we need access. The easiest way to gain access is to authenticate using github-cli
.
GitHub CLI is an open-source tool for using GitHub from your computer’s command line. When you’re working from the command line, you can use the GitHub CLI to save time and avoid switching contexts.
gh auth login
GitHub.com
and Follow the on-screen prompts.GitHub CLI automatically stores your Git credentials for you when you choose HTTPS as your preferred protocol for Git operations and answer “yes” to the prompt asking if you would like to authenticate to Git with your GitHub credentials. This can be useful as it allows you to use
git push
,git pull
, and so on, without needing to set up a separate credential manager or use SSH.
Now that you’ve initialized your Git, you need to generate your own GPG key pair and add it to your GitHub account. This is where gnupg
will help us. Open a terminal and do these:
gpg --full-generate-key
At the prompt, specify the kind of key you want, or press Enter
to accept the default. (RSA and RSA
is recommended.)
At the prompt, specify the key size you want, or press Enter
to accept the default.
Enter the length of time the key should be valid. Press Enter
to specify the default selection, indicating that the key doesn’t expire. Unless you require an expiration date. (Default is recommended.)
Verify that your selections are correct.
Enter your information.
Note: When asked to enter your email address, ensure that you enter the verified email address for your GitHub account.
Congratulations, we’ve generated your GPG key pair. Now, you need to copy your public key to add it to your GitHub account.
gpg --list-secret-keys --keyid-format=long
command to list the long form of the GPG keys for which you have both a public and private key.Note: Some GPG installations on Linux may require you to use
gpg2 --list-keys --keyid-format LONG
to view a list of your existing keys instead. In this case, you will also need to configure Git to usegpg2
by runninggit config --global gpg.program gpg2
.
$ gpg --list-secret-keys --keyid-format=long
/Users/hubot/.gnupg/secring.gpg
------------------------------------
sec 4096R/3AA5C34371567BD2 2037-03-10 [expires: 2037-05-10]
uid YOUR NAME <YOUR@EMAIL.COM>
ssb 4096R/4BB6D45482678BE3 2037-03-10
gpg --armor --export 3AA5C34371567BD2
Prints the GPG key ID, in ASCII armor format.
-----BEGIN PGP PUBLIC KEY BLOCK-----
and ending with -----END PGP PUBLIC KEY BLOCK-----
.Now that you have copied your public key, head over to Step 3 to add your public key to your GitHub account.
To sign commits associated with your account on GitHub, you can add a public GPG key to your account.
You can add multiple public keys to your account on GitHub. Commits signed by any of the corresponding private keys will show as verified. If you remove a public key, any commits signed by the corresponding private key will no longer show as verified.
To verify as many of your commits as possible, you can add expired and revoked keys. If the key meets all other verification requirements, commits that were previously signed by any of the corresponding private keys will show as verified and indicate that their signing key is expired or revoked.
To add your public key to your GitHub account, open a terminal and do these:
In the upper-right corner of any page, click your profile photo, then click Settings.
In the “Access” section of the sidebar, click SSH and GPG keys.
Next to the “GPG keys” header, click New GPG key.
In the “Title” field, type a name for your GPG key.
In the “Key” field, paste the GPG key you copied when you generated your GPG key in Step 2.1.
Click Add GPG key.
To confirm the action, authenticate to your GitHub account.
And you’re done adding your public key to your GitHub account. Now you have to tell git
about your GPG key.
git
about your GPG key.If you’re using a GPG key that matches your committer identity and your verified email address associated with your account on GitHub.com, then you can begin signing commits and signing tags.
Open a terminal and follow along:
gpg --list-secret-keys --keyid-format=long
command to list the long form of the GPG keys for which you have both a public and private key. Private key is required for signing commits.Note: Some GPG installations on Linux may require you to use
gpg2 --list-keys --keyid-format LONG
to view a list of your existing keys instead. In this case, you will also need to configure Git to usegpg2
by runninggit config --global gpg.program gpg2
.
$ gpg --list-secret-keys --keyid-format=long
/Users/hubot/.gnupg/secring.gpg
------------------------------------
sec 4096R/3AA5C34371567BD2 2016-03-10 [expires: 2017-03-10]
uid Hubot <hubot@example.com>
ssb 4096R/4BB6D45482678BE3 2016-03-10
git config --global user.signingkey 3AA5C34371567BD2
git config --global commit.gpgsign true
[ -f ~/.bashrc ] && echo -e '\nexport GPG_TTY=$(tty)' >> ~/.bashrc
Now you’ve completed setting up your GPG key pair to sign your Git commits on GitHub.
Try and make some commits, push them to GitHub and Check them out. There must be a little “Verified” tag on the commits you’ve made. 😃