Question How do I sign git commits using an IDE like IntelliJ on Windows?
If you're interested, read what I tried here:
I followed mainly Github's guide. I made sure to not forget to restart bash/IntelliJ after you changed config files.
- Generate a new GPG key pair
- Add the GPG key to my GitHub account
- Associate a verified (by GitHub) email with my GPG key where I made sure the email in my
.gitconfigis the same. - Tell Git about my GPG key
- Sign a commit with GPG and confirmed that it was Verfied on Github.
- From that same page, I set commits to be signed by default with
git config --global commit.gpgsign true(I'm using git 2.12). I made a new commit and verified it was signed withgit verify-commit HEAD When I try to commit in IntelliJ, I get
gpg: cannot open tty 'no tty'so I found IntelliJ fails to commit changes when attempting to sign commit (GPG) and addedno-ttyto myC:\Users\username\.gnupg\gpg.conffile and restarted.Then I get the error
gpg: Sorry, no terminal at all requested - can't get inputwhich seems reasonable because I just added the option that has something to do with no terminal. Like gpg: Sorry, no terminal at all requested - can't get input says the solution is to remove theno-ttywhich I hope doesn't apply to my case.Other answers in the first question suggested to add
use-agentas well in thegpg.conffile, which results in the additional errorgpg: gpg-agent is not available in this session. Ah wait, maybe I need to setup gpg-agent.The best guide for Windows that I could find was the Archlinux wiki (yeah, right). It specifies to add to
C:/Users/username/.gnupg/gpg-agent.confthe time to live, so I create that file and adddefault-cache-ttl 34560000andmax-cache-ttl 34560000as per https://superuser.com/questions/624343/keep-gnupg-credentials-cached-for-entire-user-sessionNow let's actually start this gpg-agent, https://superuser.com/questions/1153101/why-does-git-complain-that-no-gpg-agent-is-running made me check that indeed
gpg-agent --versionwas much newer thangpg --version, so I would like to have gpg2 so I could rungit config --global gpg.program gpg2.But I do not have gpg2 available on the command line. I installed Gpg4win (binary releases, at the bottom) and even Gnupg 2 separately but that didn't give me gpg2 on the command line, I noticed I had to folder GNU in my Program Files (x86) which I think I should have. With
where gpgI found out it was at least not pointing to the gpg I just downloaded, because that one showed second. So I pointed git to the right one withgit config --global gpg.program 'C:\Program Files (x86)\GnuPG\bin\gpg.exe'Now I have the error
gpg: skipped "keyid": secret key not available. The solution in gpg: skipped "N": secret key not available is what I just did, so that doesn't help. Then I realised I set everything up with the other gpg, not with this one. I didalias gpg="'C:\Program Files (x86)\GnuPG\bin\gpg.exe'", checkedgpg --versionand did the whole thing again. Actually I put the alias line in my.bash_profileso I don't need to run it every time.When I try
gpg --gen-keyit hangs immediately. No idea why, I don't think the problem is not enough entropy because the older gpg worked fine but it's possible that the newer version requires more entropy. In any case I couldn't find any windows user with the same problem on the Internet.It works! When I commit in IntelliJ, it asks for my passphrase with pinentry only once. But now I can't commit from Git Bash, with the
no secret keyerror which makes sense because as I saidgpg --list-keysis empty: no key is associated with this gpg.Intellij IDEA signing GIT commits with GPG is relevant, but the only answer is for MAC and doesn't seem to apply to Windows. It did lead me to:
me.m01.eu hints at adding a new environment variable called
GNUPGHOMEwhich points toC:\Users\username\.gnupg. That directory exists, but as mentioned in an answer from git commit signing failed: secret key not available I think my new gpg usesC:\Users\username\AppData\Roaming\gnupgso I added that instead. I checked withprintenv GNUPGHOMEthat I added it correctly (I had to reboot). Didn't change anything though.Since my keys are I think in
C:\Users\username\.gnupgI tried pointing the environment variable to there, but it didn't help,gpg --list-keyswas still empty. So I had to find another way of pointing out to gpg where my keys are.gpg --list-keys --homedir='C:/Users/s156757/.gnupg'did give the correct keys, so I decided to addhomedir C:\Users\s156757\.gnupgto myC:\Users\username\AppData\Roaming\gnupg\gpg.conffile which I had to create. Because of this bug. I confirmedgpg --list-keysreturned my keys. Still the same error, addingno-ttyanduse-agentto this conf file didn't help.
I can now commit from within IntelliJ but not anymore with Git Bash, which results in
skipped "keyid": No secret key.
More thoughts
- I didn't manage to export keys from gpg to gpg2 with
gpg --export [ID] | gpg2 --import && gpg --export-secret-keys [ID] | gpg2 --import - I thought Gpg4win alone maybe could be configured so I don't need to type my password every time. Unfortunately, I couldn't find anywhere a way to make Gpg4win remember my passphrase.
- I later thought, if I remember correctly
gpg --list-secret-keyshas never returned anything for gpg 2. I only later found out that that command is different fromgpg --list-keys.
In short this is the main problem: gpg-agent allows passphrase caching but the gpg version of git doesn't match the gpg-agent version so you have to install the right gpg 2 yourself first. But I didn't manage to do that installation in such a way that I could commit from both Git Bash and IntelliJ.
