Generating GPG Keys and Signing Commits

Generating a GPG key and using it to sign your Git commits is a recommended practice to ensure the authenticity and integrity of your code. In this post, we’ll explore the step-by-step process.

1. Installing GPG

First, you need to install G…


This content originally appeared on DEV Community and was authored by Mateus V. Farias

Generating a GPG key and using it to sign your Git commits is a recommended practice to ensure the authenticity and integrity of your code. In this post, we'll explore the step-by-step process.

1. Installing GPG

First, you need to install GPG (GNU Privacy Guard) on your machine. Depending on your operating system, you can use the following commands:

  • For Ubuntu/Debian: sudo apt-get install gnupg
  • For macOS: brew install gnupg
  • For Windows: Download and install Gpg4win from the official website.

2. Checking Existing Keys

Before generating a new GPG key, it's advisable to check if any existing keys have been generated on your machine. To do this, run the following command:

gpg --list-keys

If no keys are listed, you can proceed to generate a new GPG key.

3. Generating a GPG Key

Once GPG is installed, the next step is to generate a new key. Run the following command in the terminal:

gpg --full-generate-key

You will be prompted to provide some information such as the type of key, size, validity period, name, and email. Follow the on-screen instructions to complete the process.

4. Exporting the Public Key

After generating your key, you will need to export the public key to associate it with your profile on GitHub or another Git service. Use the command below to export your public key:

gpg --armor --export YOUR_KEY_ID

Replace "YOUR_KEY_ID" with the ID of your key, which can be found with the command gpg --list-keys.

5. Linking the Key to GitHub

Copy the output of the previous command and go to your account settings on GitHub. In the "SSH and GPG keys" section, add a new GPG key and paste the public key that you copied.

To get the ID of your key, execute the command:

gpg --list-secret-key --keyid-format LONG

After obtaining the key ID, use the command below to export the public key:

gpg --armor --export YOUR_KEY_ID

Replace "YOUR_KEY_ID" with the ID of your key. Copy the output of this command and add the GPG public key to GitHub or another Git service.

GitHub Docs: Adding a GPG key to your GitHub account

6. Setting Up Git to Use the GPG Key

Now, you need to configure Git to sign your commits with your GPG key. Execute the following commands replacing "YOUR_KEY_ID" with your key ID:

git config --global user.signingkey YOUR_KEY_ID
git config --global commit.gpgSign true

7. Signing Commits

With all configurations completed, you can start signing your commits. When making a commit, Git automatically uses your GPG key to sign it:

git commit -m "Your commit message"

To verify if the commit was signed correctly, you can use the command:

git log --show-signature

Conclusion

Signing your commits with a GPG key is an excellent way to enhance the security and reliability of your code. This guide should help you set up and effectively use GPG keys to sign your commits.


This content originally appeared on DEV Community and was authored by Mateus V. Farias


Print Share Comment Cite Upload Translate Updates
APA

Mateus V. Farias | Sciencx (2024-07-24T21:56:57+00:00) Generating GPG Keys and Signing Commits. Retrieved from https://www.scien.cx/2024/07/24/generating-gpg-keys-and-signing-commits/

MLA
" » Generating GPG Keys and Signing Commits." Mateus V. Farias | Sciencx - Wednesday July 24, 2024, https://www.scien.cx/2024/07/24/generating-gpg-keys-and-signing-commits/
HARVARD
Mateus V. Farias | Sciencx Wednesday July 24, 2024 » Generating GPG Keys and Signing Commits., viewed ,<https://www.scien.cx/2024/07/24/generating-gpg-keys-and-signing-commits/>
VANCOUVER
Mateus V. Farias | Sciencx - » Generating GPG Keys and Signing Commits. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/07/24/generating-gpg-keys-and-signing-commits/
CHICAGO
" » Generating GPG Keys and Signing Commits." Mateus V. Farias | Sciencx - Accessed . https://www.scien.cx/2024/07/24/generating-gpg-keys-and-signing-commits/
IEEE
" » Generating GPG Keys and Signing Commits." Mateus V. Farias | Sciencx [Online]. Available: https://www.scien.cx/2024/07/24/generating-gpg-keys-and-signing-commits/. [Accessed: ]
rf:citation
» Generating GPG Keys and Signing Commits | Mateus V. Farias | Sciencx | https://www.scien.cx/2024/07/24/generating-gpg-keys-and-signing-commits/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.