|
| 1 | +--- |
| 2 | +layout: post |
| 3 | +title: Signing Git Commits in Windows |
| 4 | +subtitle: How to sign your Git commits in Windows using GPG and Kleopatra |
| 5 | +gh-repo: LizardByte/LizardByte.github.io |
| 6 | +gh-badge: [follow, star] |
| 7 | +tags: [dev, git, github, guide, windows] |
| 8 | +comments: true |
| 9 | +author: ReenigneArcher |
| 10 | +--- |
| 11 | + |
| 12 | +## Introduction |
| 13 | +Have you ever wanted to sign your Git commits, and get the fancy `Verified` badge on your commits in GitHub? |
| 14 | +This post will show you how to sign your Git commits in Windows using GPG and Kleopatra. |
| 15 | + |
| 16 | +## Prerequisites |
| 17 | +1. Download and install [Gpg4win](https://www.gpg4win.org/), ensuring that Kleopatra option is selected. |
| 18 | + |
| 19 | +## Create new OpenPGP certificate |
| 20 | +1. Open Kleopatra and select "New Key Pair" |
| 21 | + |
| 22 | +  |
| 23 | + |
| 24 | +2. Enter your GitHub username and email. The email must be one you have assigned to your GitHub account. |
| 25 | + You can find your no-reply email https://github.com/settings/emails on this page if you have one set. |
| 26 | +3. Optionally, expand "Advanced Options" and change the "Key Material" option. I used "rsa4096". |
| 27 | + You can also change or disable the expiration date. |
| 28 | +4. Select "OK". |
| 29 | + |
| 30 | +## Configure git |
| 31 | +1. Open terminal (cmd.exe or PowerShell) and run the following command. |
| 32 | + |
| 33 | + ```bash |
| 34 | + gpg --list-secret-keys --keyid-format LONG |
| 35 | + ``` |
| 36 | + |
| 37 | +2. Copy the key id. `1234567890ABCDEF` is the id in the example below. |
| 38 | + |
| 39 | + ``` |
| 40 | + sec rsa4096/1234567890ABCDEF 2024-12-08 [SC] |
| 41 | + ABCDEF1234567890ABCDEFGH1234567890ABCDEF |
| 42 | + uid [ultimate] GitHub-Username <GitHub-Email-Address> |
| 43 | + ssb rsa4096/0987654321FEDCBA 2024-12-08 [E] |
| 44 | + ``` |
| 45 | + |
| 46 | +3. Run the following command to configure git to use the key. |
| 47 | + |
| 48 | + ```bash |
| 49 | + git config --global user.signingkey <KEY-ID> |
| 50 | + ``` |
| 51 | + |
| 52 | +4. If git cannot find gpg you may need to add the path to the gpg executable to your git configuration. |
| 53 | + You can find the path to gpg by running the following command. |
| 54 | + |
| 55 | + ```bash |
| 56 | + where gpg |
| 57 | + ``` |
| 58 | + |
| 59 | + Then add the path to your git configuration. |
| 60 | + |
| 61 | + ```bash |
| 62 | + git config --global gpg.program "C:\Program Files (x86)\GnuPG\bin\gpg.exe" |
| 63 | + ``` |
| 64 | + |
| 65 | +5. Run the following command to enable commit signing. |
| 66 | + |
| 67 | + ```bash |
| 68 | + git config --global commit.gpgsign true |
| 69 | + ``` |
| 70 | + |
| 71 | +## Configure GitHub |
| 72 | +1. Right-click the key in Kleopatra, select "Details", then "Export". Select all the contents and copy them. |
| 73 | +2. Go to your GitHub account settings, and select "SSH and GPG keys". |
| 74 | +3. Click "New GPG key". Give the key a title, and paste the contents of the public key into the key field. |
| 75 | +4. Click "Add GPG key". |
| 76 | + |
| 77 | +## Automatically start Kleopatra |
| 78 | +1. Open the Windows Task Scheduler. |
| 79 | +2. Optionally, create a new folder for the task. |
| 80 | +3. Select the folder, and then click "Create Task". |
| 81 | +4. Enter a name for the task, such as "Open Kleopatra". |
| 82 | +5. Select "Run only when user is logged on". |
| 83 | +6. Click the "Triggers" tab, then "New". |
| 84 | +7. Select "At log on" from the "Begin the task" dropdown. |
| 85 | +8. Select "Specific user", and click "OK". |
| 86 | +9. Click the "Actions" tab, then "New". |
| 87 | +10. Click "Browse", and select the Kleopatra executable. |
| 88 | + The default location is `"C:\Program Files (x86)\Gpg4win\bin\kleopatra.exe"` |
| 89 | +11. In the "Start in" field, enter `%HOMEDRIVE%%HOMEPATH%`, and click "OK". |
| 90 | +12. On the "Conditions" tab, uncheck everything. |
| 91 | +13. On the settings tab, ensure the following options are set. |
| 92 | + |
| 93 | + - Allow task to be run on demand (checked) |
| 94 | + - Run task as soon as possible after a scheduled start is missed (checked) |
| 95 | + - If the task fails, restart every 1 minute (checked) |
| 96 | + - Attempt to restart up to 3 times |
| 97 | + - Stop the task if it runs longer than (unchecked) |
| 98 | + - If the running task does not end when requested, force it to stop (checked) |
| 99 | + - If the task is not scheduled to run again, delete it after (unchecked) |
| 100 | + - If the task is already running, then the following rule applies (Do not start a new instance) |
| 101 | + |
| 102 | +14. Click "OK". |
| 103 | + |
| 104 | +## Revoking a Key |
| 105 | +In some cases you may need to revoke a key. To do this follow the steps below. |
| 106 | + |
| 107 | +1. Open Kleopatra, right-click on the key, and select "Revoke Certificate". |
| 108 | +2. Select the reason for revoking the key, and click "Revoke Certificate". |
| 109 | +3. Right-click the key again, select "Details", then "Export". Select all the contents and copy them. |
| 110 | +4. Go to your GitHub account settings, and select "SSH and GPG keys". |
| 111 | +5. Remove the old key, and click "New GPG key". Give the key a title (such as "Revoked Key"), |
| 112 | + and paste the contents of the public key into the key field. |
| 113 | + |
| 114 | +Alternatively, this can be done via command line. |
| 115 | +See [How to actually revoke a GitHub GPG key](https://github.com/orgs/community/discussions/108355#discussioncomment-8476035) |
| 116 | + |
| 117 | +This approach will allow previously verified commits to remain verified. |
| 118 | + |
| 119 | +## Caveats |
| 120 | +- Commits made before your key was created cannot be verified, even if you amend the commits. This can be annoying if |
| 121 | + you have any WIP commits that you want to amend. A simple workaround for this is to start an interactive rebase, |
| 122 | + and edit each commit to adjust. Finally, when you amend the commit do so with the following command. |
| 123 | + |
| 124 | + ```bash |
| 125 | + git commit --amend --date=now --no-edit |
| 126 | + ``` |
| 127 | + |
| 128 | +## Conclusion |
| 129 | +You should now be able to sign your Git commits in Windows using GPG and Kleopatra. I'd like to give credit to |
| 130 | +Tom Auger for his [blog post](https://tau.gr/posts/2018-06-29-how-to-set-up-signing-commits-with-git/) which helped me |
| 131 | +figure out how to do this initially. |
0 commit comments