|
| 1 | +--- |
| 2 | +title: Signing commits with GPG |
| 3 | +--- |
| 4 | + |
| 5 | +<h1>Signing commits with GPG</h1> |
| 6 | + |
| 7 | +`git` has an authentication layer to control who can push commits to a repo, but it does not authenticate the actual commit authors. |
| 8 | + |
| 9 | +In other words, you can commit changes as `Elon Musk <elon@tesla.com>`, push them to your preferred `git` host (for instance github.com), and your commit will link to Elon's GitHub profile. (Try it! But don't blame us if Elon gets mad at you for impersonating him.) |
| 10 | + |
| 11 | +The reasons we implemented GPG signing were: |
| 12 | +- To provide finer-grained security, especially as more and more Enterprise users rely on the Hub. |
| 13 | +- To provide ML benchmarks backed by a cryptographically-secure source. |
| 14 | + |
| 15 | +See Ale Segala's [How (and why) to sign `git` commits](https://withblue.ink/2020/05/17/how-and-why-to-sign-git-commits.html) for more context. |
| 16 | + |
| 17 | +You can prove a commit was authored by you with GNU Privacy Guard (GPG) and a key server. GPG is a cryptographic tool used to verify the authenticity of a message's origin. We'll explain how to set this up on Hugging Face below. |
| 18 | + |
| 19 | +The Pro Git book is, as usual, a good resource about commit signing: [Pro Git: Signing your work](https://git-scm.com/book/en/v2/Git-Tools-Signing-Your-Work). |
| 20 | + |
| 21 | +## Setting up signed commits verification |
| 22 | + |
| 23 | +You will need to install [GPG](https://gnupg.org/) on your system in order to execute the following commands. |
| 24 | +> It's included by default in most Linux distributions. |
| 25 | +> On Windows, it is included in Git Bash (which comes with `git` for Windows). |
| 26 | +
|
| 27 | +You can sign your commits locally using [GPG](https://gnupg.org/). |
| 28 | +Then configure your profile to mark these commits as **verified** on the Hub, |
| 29 | +so other people can be confident that they come from a trusted source. |
| 30 | + |
| 31 | +For a more in-depth explanation of how git and GPG interact, please visit the the [git documentation on the subject](https://git-scm.com/book/en/v2/Git-Tools-Signing-Your-Work) |
| 32 | + |
| 33 | +Commits can have the following signing statuses: |
| 34 | + |
| 35 | +| Status | Explanation | |
| 36 | +| ----------------- | ------------------------------------------------------------ | |
| 37 | +| Verified | The commit is signed and the signature is verified | |
| 38 | +| Unverified | The commit is signed but the signature could not be verified | |
| 39 | +| No signing status | The commit is not signed | |
| 40 | + |
| 41 | +For a commit to be marked as **verified**, you need to upload the public key used to sign it on your Hugging Face account. |
| 42 | + |
| 43 | +Use the `gpg --list-secret-keys` command to list the GPG keys for which you have both a public and private key. |
| 44 | +A private key is required for signing commits or tags. |
| 45 | + |
| 46 | +If you don't have a GPG key pair or you don't want to use the existing keys to sign your commits, go to **Generating a new GPG key**. |
| 47 | + |
| 48 | +Otherwise, go straight to [Adding a GPG key to your account](#adding-a-gpg-key-to-your-account). |
| 49 | + |
| 50 | +## Generating a new GPG key |
| 51 | + |
| 52 | +To generate a GPG key, run the following: |
| 53 | + |
| 54 | +```bash |
| 55 | +gpg --gen-key |
| 56 | +``` |
| 57 | + |
| 58 | +GPG will then guide you through the process of creating a GPG key pair. |
| 59 | + |
| 60 | +Make sure you specify an email address for this key, and that the email address matches the one you specified in your Hugging Face [account](https://huggingface.co/settings/account). |
| 61 | + |
| 62 | +## Adding a GPG key to your account |
| 63 | + |
| 64 | +1. First, select or generate a GPG key on your computer. Make sure the email address of the key matches the one in your Hugging Face [account](https://huggingface.co/settings/account) and that the email of your account is verified. |
| 65 | + |
| 66 | +2. Export the public part of the selected key: |
| 67 | + |
| 68 | +```bash |
| 69 | +gpg --armor --export <YOUR KEY ID> |
| 70 | +``` |
| 71 | + |
| 72 | +3. Then visit your profile [settings page](https://huggingface.co/settings/keys) and click on **Add GPG Key**. |
| 73 | + |
| 74 | +Copy & paste the output of the `gpg --export` command in the text area and click on **Add Key**. |
| 75 | + |
| 76 | +4. Congratulations! 🎉 You've just added a GPG key to your account! |
| 77 | + |
| 78 | +## Configure git to sign your commits with GPG |
| 79 | + |
| 80 | +The last step is to configure git to sign your commits: |
| 81 | + |
| 82 | +```bash |
| 83 | +git config user.signingkey <Your GPG Key ID> |
| 84 | +git config user.email <Your email on hf.co> |
| 85 | +``` |
| 86 | + |
| 87 | +Then add the `-S` flag to your `git commit` commands to sign your commits! |
| 88 | + |
| 89 | +```bash |
| 90 | +git commit -S -m "My first signed commit" |
| 91 | +``` |
| 92 | + |
| 93 | +Once pushed on the Hub, you should see the commit with a "Verified" badge. |
0 commit comments