How to Sign Your Commits: A Guide for Git Users
Tags: git, github, securecoding
Published: 2024-04-04 15:16:25

---
title: How to Sign Your Commits: A Guide for Git Users
published: true
date: 2024-04-04 15:07:19 UTC
tags: 
canonical_url: https://imransaifi.hashnode.dev/how-to-sign-your-commits-a-guide-for-git-users
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aondqxk34o8av2qicmo8.png
tags: git,,github,,SecureCoding
---

## **Why Sign Your Commits?**

Before learning how to sign commits, we will learn why signing your commits is important.

Commit signing offers several benefits for both individual developers and collaborative projects:

1. **Code Integrity** : Signing your commits verifies that they were indeed authored by you and have not been tampered with since. This helps maintain the integrity of the codebase and ensures that only trusted changes are accepted.

2. **Attribution** : Signed commits provide clear attribution, allowing project maintainers and collaborators to identify the author of each change accurately.

3. **Trust and Verification** : By signing your commits with a cryptographic key, you establish trust in your contributions. Other developers can verify the authenticity of your commits using your public key, thereby increasing confidence in the codebase.

4. **Compliance and Audit Trails** : In certain industries or projects with strict regulatory requirements, commit signing helps maintain compliance and provides an audit trail for all changes made to the codebase.

## **How to Sign Your Commits**

Signing your commits involves a few simple steps. Below, we outline the process using Git and GPG (GNU Privacy Guard), a widely-used open-source encryption software:

#### Step 1: Install GPG

If you haven't already, install GPG on your system. You can download and install GPG from the official website for Windows and MacOS or use your package manager for Linux distributions.

I am using Ubuntu. GPG is already installed on that. Check it by using the `gpg --help` command.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1712214132053/09e64106-5974-435b-8757-ae6a087cd817.png)

#### Step 2: Generate a GPG Key Pair

Generate a new GPG key pair using the `gpg --full-generate-key` command. Follow the prompts to enter your type of key, key bit size, expiration time, name, email address, and passphrase. This passphrase will be used to protect your private key, so choose a strong and memorable one.

💡

Recommendation: the key size should be 4096 bits as its the most secure.

💡

Recommendation: select key type 1 that is RSA and RSA which will be helpful in encrypting the commits and alot of other things and not just signing the commits

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1712228858884/687e43f9-5f8b-43e0-844b-e4a7871685e4.png)

#### Step 3: List Your GPG Keys

List the GPG keys on your system using `gpg --list-secret-keys --keyid-format LONG`. This command will display a list of GPG keys along with their associated IDs.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1712230026272/e801b049-7a5f-4060-a36f-a7937f1d8b93.png)

#### Step 4: Configure Git to Use Your GPG Key

Configure Git to use your GPG key for signing commits by running the following commands:

```
git config --global user.signingkey <GPG_KEY_ID>git config --global commit.gpgsign true
```

Replace `<GPG_KEY_ID>` with the ID of your GPG key, which you obtained from the previous step.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1712231072458/b9368ac5-deb0-4c9f-857b-1ac2dabace53.png)

#### Step 5: Make a Signed Commit

Now, whenever you make a commit, add the `-S` flag to sign it with your GPG key:

```
git commit -S -m "Your commit message"
```

Your commit is now signed with your GPG key, and the signature is embedded in the commit metadata.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/upripqeqkfz6tig19xtl.png)

#### Step 6: Share Your Public Key

To allow others to verify your signed commits, share your GPG public key with them. You can export your public key using:

```
gpg --armor --export <YOUR_EMAIL>
```

Replace `<YOUR_EMAIL>` with the email address associated with your GPG key. Share the exported public key through key servers, email, or other secure channels.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1712232124484/37f1ea13-7a34-41c2-8c10-fa043586e2ea.png)

#### Step 7: Add your GPG key to your Github account

To enable GitHub to recognize your signed commits and display a "Verified" badge next to them, you need to add your GPG key to your GitHub account.

**Copy Your GPG Key** : Copy the output including `-----BEGIN PGP PUBLIC KEY BLOCK-----` and `-----END PGP PUBLIC KEY BLOCK-----` from the previous step

Go to your `GitHub settings > SSH and GPG keys > New GPG key`

**Paste Your GPG Key** : Paste your GPG public key into the `Key` field.

Click `Add GPG key` to save it to your GitHub account.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1712232808968/39041982-6f89-4b8c-ab2e-33b7c04f61f0.png)

Now Your GPG key is added to you GitHub account.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1712232829650/defbff92-9e8c-4093-aace-15eba0c2d82f.png)

Once you've added your GPG key to your GitHub account, GitHub will recognize your signed commits and display a "Verified" badge next to them. This provides additional validation of your commits' authenticity to other contributors and users of the repository.

## **Conclusion**

Signing your commits is a simple yet effective way to enhance the security and trustworthiness of your contributions to a Git repository. By following the steps outlined in this guide, you can start signing your commits today and contribute to a more secure and transparent development process. Whether you're working on open-source projects or proprietary software, commit signing helps ensure the integrity and authenticity of your code changes, fostering a culture of trust and collaboration within the developer community.