refactor: finish moving ssh-* scripts to own installers
[webi-installers/.git] / git-config-gpg / README.md
1 ---
2 title: git-config-gpg
3 homepage: https://webinstall.dev/git-config-gpg
4 tagline: |
5   Get your GnuPG Public Key.
6 ---
7
8 ## Cheat Sheet
9
10 > Although the latest git release allows you to sign with SSH Keys (and GitHub
11 > will implement this shortly if it hasn't already), most systems do not have
12 > the latest git release, and most verification systems are not updated with the
13 > newest verification techniques, so you may wish to sign your commits with GPG,
14 > as has been done for the last 20 years...
15
16 Here we'll cover
17
18 - How to [add a GPG key to Github](https://github.com/settings/gpg/new)
19 - How to cache the passphrase longer
20 - How to [create a GPG key](./gpg-pubkey)
21 - How to configure git with GPG signing
22 - Troubleshooting 'gpg failed to sign the data'
23
24 Usage:
25
26 ```bash
27 git-config-gpg
28 ```
29
30 Example output:
31
32 ```txt
33 GnuPG Public Key ID: CA025BC42F00BBBE
34
35 -----BEGIN PGP PUBLIC KEY BLOCK-----
36
37 mQGNBGGQtKIBDAChxTT375fetQawLkyyDcz07uIEZVa9pvuip8goMqev7PkOIHi+
38 j6PDtFmxgv8ZOFe8+1RfMC7eL5fYah0/OBxNm7pPvAPDWOX38FfUzoq9CALW2xPD
39 ...
40 Yee+eokiC2mWIEkMwbqlnNmkX/wphS0zcCsEiHirmDxgY6YY9QRjlzUMY68OqjfJ
41 IFjFWv3R7eckM957wyR5BvdQNfGrW7cWefWhdZOzLEE7
42 =GXEK
43 -----END PGP PUBLIC KEY BLOCK-----
44
45 Successfully updated ~/.gitconfig for gpg commit signing
46
47 How to verify signed commits on GitHub:
48
49     1. Go to 'Add GPG Key': https://github.com/settings/gpg/new
50     2. Copy and paste the key above from the first ---- to the last ----
51 ```
52
53 ### Files
54
55 These are the files / directories that are created and/or modified with this
56 install:
57
58 ```txt
59 ~/.config/envman/PATH.env
60 ~/.local/bin/git-config-gpg
61 ~/Downloads/YOU.KEY_ID.gpg.asc
62 ```
63
64 ### How to add your GPG Public Key to GitHub
65
66 1. Go to your GitHub Profile (<https://github.com/settings/profile>)
67 2. Go to the SSH and GPG Keys (<https://github.com/settings/keys>)
68 3. Add GPG Key (<https://github.com/settings/gpg/new>)
69 4. Paste the output of `gpg-pubkey` into the form
70
71 ### How to cache the Passphrase longer
72
73 If you'd like the passphrase to be cached until your login session ends, just
74 set it to 400 days and call it good.
75
76 `~/.gnupg/gpg-agent.conf`:
77
78 ```txt
79 default-cache-ttl 34560000
80 max-cache-ttl 34560000
81 ```
82
83 You'll need to reload `gpg-agent` for this to take effect, or just logout and
84 login again.
85
86 ```bash
87 # kill gpg-agent dead
88 killall gpg-agent
89 gpgconf killall gpg-agent
90
91 # start gpg-agent again (yes, 'bye' to start)
92 gpg-connect-agent --agent-program ~/.local/opt/gnupg/bin/gpg-agent /bye
93 ```
94
95 Note: You may need to change or omit `--agent-program`, depending on how you
96 installed `gpg` (if you installed it with Webi, run it as shown above).
97
98 ### How to create a GPG Key
99
100 See:
101
102 - [gpg-pubkey](./gpg-pubkey)
103 - and [gpg](./gpg), if you want to do it "the hard way"
104
105 ### How to manually set up git commit gpg signing
106
107 (this is what `git-config-gpg` does)
108
109 Run [gpg-pubkey-id](./gpg-pubkey) to get your GnuPG Public Key ID and then
110 update your `~/.gitconfig` to sign with it by default:
111
112 ```bash
113 #!/bin/bash
114
115 MY_KEY_ID="$(
116   gpg-pubkey-id
117 )"
118
119 git config --global user.signingkey "${MY_KEY_ID}"
120 git config --global commit.gpgsign true
121 git config --global log.showSignature true
122 ```
123
124 Or, for Windows users:
125
126 ```bash
127 #!/usr/bin/env pwsh
128
129 $my_key_id = gpg-pubkey-id
130
131 git config --global user.signingkey "$my_key_id"
132 git config --global commit.gpgsign true
133 git config --global log.showSignature true
134 ```
135
136 Or, if you prefer to edit the text file directly:
137
138 `~/.gitconfig`
139
140 ```txt
141 [user]
142   signingkey = CA025BC42F00BBBE
143 [commit]
144   gpgsign = true
145 [log]
146   showSignature = true
147 ```
148
149 In some cases you may also want to prevent conflicts between different installed
150 versions of gpg, like so:
151
152 ```bash
153 git config --global gpg.program ~/.local/opt/gnupg/bin/gpg
154 ```
155
156 ```txt
157 [gpg]
158   program = /Users/me/.local/opt/gnupg/bin/gpg
159 ```
160
161 ### Troubleshooting 'gpg failed to sign the data'
162
163 `gpg` is generally expected to be used with a Desktop client. On Linux servers
164 you may get this error:
165
166 ```txt
167 error: gpg failed to sign the data
168 fatal: failed to write commit object
169 ```
170
171 Try to load the `gpg-agent`, set `GPG_TTY`, and then run a clearsign test.
172
173 ```bash
174 gpg-connect-agent /bye
175 export GPG_TTY=$(tty)
176 echo "test" | gpg --clearsign
177 ```
178
179 If that works, update your `~/.bashrc`, `~/.zshrc`, and/or
180 `~/.config/fish/config.fish` to include the following:
181
182 ```bash
183 gpg-connect-agent /bye
184 export GPG_TTY=$(tty)
185 ```
186
187 If this is failing on Mac or Windows, then `gpg-agent` is not starting as
188 expected on login (for Mac the above may work), and/or the `pinentry` command is
189 not in the PATH.
190
191 If you just installed `gpg`, try closing and reopening your Terminal, or
192 possibly rebooting.