192

I am getting this error when trying to commit using Git.

gpg: skipped "name <name@mail.com>": secret key not available
gpg: signing failed: secret key not available
error: gpg failed to sign the data
fatal: failed to write commit object

I have generated a new key as below but it still gives the same error

gpg --list-keys
~/.gnupg/pubring.gpg
--------------------------------
pub   2048R/35F5FFB2 2016-04-23
uid                  name (New key) <name@mail.com>
sub   2048R/112A8C2D 2016-04-23

The secret key is the same as above

I have found this Generating a GPG key for git tagging and followed the steps but it still doesn't work, any idea?

1
  • 8
    For Windows users, it's much more likely encountering this error because Git on Windows doesn't use the correct gpg. Not finding the correct binary in PATH, git resorts to using internally bundled gpg inside its minified MSYS, which has no knowledge of where your keys are. Setting gpg.program or G(NU)PGHOME variables would resolve it, as specified in a few of the answers below. May 28, 2020 at 19:06

13 Answers 13

307

This worked for me on Windows 10 (Note that I use the absolute path to gpg.exe):

git config --global gpg.program "C:\Program Files (x86)\GnuPG\bin\gpg.exe"

This was the error I got prior to the fix:

gpg: skipped "3E81C*******": secret key not available
gpg: signing failed: secret key not available
error: gpg failed to sign the data
fatal: failed to write commit object
8
  • 13
    Got this error secret key not available with VS Code and setting gpg.exe location solved it.
    – Dawid
    Aug 6, 2018 at 22:37
  • 2
    This worked for me as well. Especially after following the configuring steps from github itself.
    – Bas G
    Aug 21, 2019 at 6:46
  • 1
    Every time my Windows Insider build installs, this option resets. Thanks for this :)
    – VRG
    Jan 8, 2020 at 13:09
  • This worked for me as well after adding the absolute path of gpg. Thanks a lot. Oct 16, 2020 at 11:58
  • 8
    yes, this is important as git has own gpg and most people will install gpg for windows and import priv key via kleopatra and only "main" gpg is aware of key location. Great answer Dec 23, 2020 at 17:20
139
+100

You need to configure the secret key before using it.

git config user.signingkey 35F5FFB2

Or declare it globally if you want to use the same key for every repository.

git config --global user.signingkey 35F5FFB2

Source: Git Tools - Signing Your Work

9
  • Thank you vey much Leonardo! It works!! Sorry if the question was silly, I had no idea how to solve it. Apr 23, 2016 at 14:22
  • 1
    Is it secure to store .gitconfig with secret key in public repo?
    – melihovv
    Feb 2, 2017 at 0:59
  • 1
    for the record as @melihovv asked about: — The secret key must be on your GPG keyring. Only the hash ID of that key is stored in the config.
    – BRPocock
    May 16, 2017 at 14:21
  • 5
    I had to do this along with git config --global gpg.program gpg2.
    – Splaktar
    Sep 27, 2017 at 6:06
  • 1
    I get a passphrase dialog with 1.4.22 but not with v2. As soon as I do git config --global gpg.program "C:/Program Files (x86)/GnuPG/bin/gpg.exe" it breaks and I get the no secret key errors. What to do?
    – Ini
    Sep 7, 2018 at 19:34
78

What worked for me was adding

git config --global gpg.program "C:/Program Files (x86)/GNU/GnuPG/gpg2.exe"

If you want to find the full path of gpg2.exe:

where gpg2.exe
3
  • 3
    Depending on the installed version of GnuPG, you need to do this for gpg.exe instead of gpg2.exe. Sep 7, 2018 at 14:35
  • 1
    thank you, my problem was solved using the full path (that was C:\Users\Me\AppData\Local\gnupg\bin\gpg.exe) Jan 24, 2021 at 15:09
  • THANK YOU! This worked for me. You saved my day :). I just used "where gpg.exe" as I dont have "gpg2.exe"
    – libik
    Jun 14 at 9:35
42

I recently found the same secret key not available error and a few more along the way, like GPG agent not found for instance.

In my case I wanted to get commits signed and showing as verified on GitHub.

Below are the complete steps to get it working on Windows 10 x64:

Install GPG

I installed GPG 2.3.1 with winget like so:

C:\> winget install GnuPG.GnuPG

Verify it with:

C:\> gpg --version

Generate GPG key

C:\> gpg --full-generate-key
  • Add your real name and e-mail, the same as used in the GitHub account.

  • The key must be at least 4096 bits.

Export the key in ASCII armor format

First list the key:

C:\> gpg --list-secret-keys --keyid-format=long

sec rsa4096/[short-key] 2021-06-14 [SC]

Then export it:

C:\> gpg --armor --export [short-key]

Copy the key including the BEGIN/END text.

-----BEGIN PGP PUBLIC KEY BLOCK-----
[huge-ascii-key]
-----END PGP PUBLIC KEY BLOCK-----

Add the GPG armor ASCII key to the GitHub account

Go to Profile > Settings > SSH and GPG keys > New GPG key

Or please follow these visual instructions.

Configure Git to sign all commits by default

C:\> git config --global user.signingkey [short-key]
C:\> git config --global commit.gpgsign true
C:\> git config --global gpg.program "C:/Program Files (x86)/gnupg/bin/gpg"

Set GPG environment variable for the GPG Agent

Check for GPG agent:

gpg-agent --version

Set the environment variable:

GNUPGHOME=%USERPROFILE%\AppData\Roaming\gnupg

Done

The resulting .gitconfig would have the user section like so:

[user]
    name = Your Name
    email = your@email.com
    signingkey = [short-key]
[commit]
    gpgsign = true
[gpg]
    program = C:/Program Files (x86)/gnupg/bin/gpg
3
  • 3
    thanks, this was really helpful, this should be chosen as the best answer
    – akmsw
    Jan 4 at 0:43
  • 2
    it solves my problem, after reusing "C:\Program Files\Git\usr\bin\gpg.exe", declared in PATH. Jan 30 at 9:45
  • For me the only thing missing was setting the GNUPGHOME environment variable, which I had to set to "%USERPROFILE%\.gnupg" because that's where it was installed by default. I created my key with the default 3072 bytes, not 4096 and it worked just fine.
    – Sideways S
    May 31 at 14:47
18

I'like to complete all these answers, cause I've got many issues with this.

These exemples use the --global flag, but you can remove it if you want to to these things locally.

Configure secret key in git

git config --global user.signingkey 35F5FFB2

Configure witch gpg program tu use in git (optional)

Some systems (Ubuntu for exemple) can have gpg and gpg2 at the same time. You need to specify you'll use gpg2

git config --global gpg.program gpg2

Export GPG_TTY (optional)

It is possible if you use these command in an ssh environment that you have the following error : Inappropriate ioctl for device or gpg: échec de la signature : Ioctl() inapproprié pour un périphérique. This can be fixed via :

export GPG_TTY=$(tty)

Auto enable GPG singing (optional)

git config --global commit.gpgsign true
3
  • How does one disable it if you no longer want to use it? Mar 8, 2018 at 18:53
  • 1
    You can do add the --unset flag on those git config commands to disable it
    – alphayax
    Mar 9, 2018 at 16:35
  • I actually used this git config --global commit.gpgsign true but said false. Mar 9, 2018 at 20:16
12

Using "C:\Program Files\Git\usr\bin\gpg.exe" was the solution for me.
Had to uninstall kleopatra. With it, it was not working.

So, summing up;

  • No need for kleopatra, use GIT default instead.

  • git config --global user.signingkey Y0URK3Y
    git config --global commit.gpgsign true
    git config --global gpg.program "C:\Program Files\Git\usr\bin\gpg.exe"
    
1
  • 1
    among all of these, yours, worked with me.
    – Amr
    Nov 27, 2022 at 0:32
11

I had a situation in which the same was happening to me in a Windows 10 machine.

$ git commit -m "Improve logging, imports and show time executed"
gpg: signing failed: Operation cancelled
gpg: signing failed: Operation cancelled
error: gpg failed to sign the data
fatal: failed to write commit object

The commands "C:\Program Files (x86)\GnuPG\bin\gpg.exe" --list-secret-keys --keyid-format LONG and gpg --list-secret-keys --keyid-format LONG where giving me complete different results!

$ where gpg
C:\Program Files\Git\usr\bin\gpg.exe
C:\Program Files (x86)\GnuPG\bin\gpg.exe

The main reason was related to previous answers but on a different sense:

  • I was creating the gpg keys using the git (configured path) version of GPG
  • Git was configured to use the downloaded version of gpg for the commit.
  • Seems GPG implementations use their own certificate database and storage.

I hope this can help anyone that stumbles on this message and previous answers do not solve ther issue.

2
  • Had to add the secret key to kleopatra from the cli export, as visual studio community seems to use that by default for signing, where as the git cli used a different list/implementation and hence still worked without kleopatra.
    – Nithish
    Jun 30, 2020 at 22:22
  • I copied everything from C:\Users\USERNAME\.gnupg to C:\Users\USERNAME\AppData\Roaming\gnupg and problem solved! Jan 9, 2022 at 0:23
7

You have to set the variable GNUPGHOME. Without it, GnuPG is not able to find your keys.

# On unix add it to your path

# On windows it will usually be under: 
<drive>:\Users\<username>\AppData\Roaming\gnupg

On Unix it simply adding it to the path.
On Windows you have to open the control panel and set it as

System Variable
  Name: GNUPGHOME
  Path: <drive>:\Users\<username>\AppData\Roaming\gnupg
1
  • 2
    For me it was not GNUPGHOME. With GPGHOME it works for me
    – Sysix
    May 15, 2018 at 14:31
4

The following steps helped me get it fixed:

git config --global gpg.program "C:\Program Files (x86)\GnuPG\bin\gpg.exe"

git config --global --unset gpg.format

gpg --list-secret-keys --keyid-format=long
// this will list the keys and copy the Key-ID

enter image description here

copy the higlighted key in the first line and use in this next query as Key-ID

git config --global user.signingkey <Key-ID>

git config --global commit.gpgsign true
1
  • I had to come back to this advice several times on different machines! Thank you for this solution. It was what I was missing. 7 hours ago
2

I had the same problem at it was that git name and email store in .gitconfig were different from the ones of the gpg key provided. I changed them in order to match and it started to work.

1

On Windows 11, setting the gpg.program as follows, didn't work for me even though the executable exists in that location.

git config --global gpg.program "C:\Program Files (x86)\gnupg\bin\gpg.exe"

I had to set it as follows:

git config --global gpg.program gpg
1

I got this error in VS Code. I ran below command in Bash

git config --global commit.gpgsign false

Then, hit Ctrl+, and open your settings. Uncheck "Enable Commit Signing" in workspace settings if it's enabled.

enter image description here

Now open VS Code Terminal and execute below command

enter image description here

Close and reopen VS Code if needed. It should work now.

1
  • 1
    The above worked for me
    – Alfred
    Apr 18 at 6:30
-5

Maybe you need to clone your own repository where you have rights. I had this issue when I cloned the repository of another person.

0

Not the answer you're looking for? Browse other questions tagged or ask your own question.