This error occurs when Git cannot sign a commit using GPG. Common causes include an expired GPG key, misconfigured TTY, or wrong GPG program. Fixing typically involves setting GPG_TTY, restarting gpg-agent, or reconfiguring your signing key.
When you have commit signing enabled in Git (via `commit.gpgsign = true`), Git invokes GPG to cryptographically sign each commit. The error 'gpg failed to sign the data' means GPG was unable to complete the signing process, and 'failed to write commit object' indicates that without a valid signature, Git cannot create the commit. This typically happens when GPG cannot access your private key, cannot prompt for a passphrase, or when the key itself has issues. The underlying problem is usually related to the GPG agent, terminal configuration, or key expiration rather than Git itself.
First, verify that GPG can sign data outside of Git:
echo "test" | gpg --clearsignIf this fails, the problem is with GPG itself, not Git. If it succeeds and shows a PGP signature block, proceed to check Git configuration.
GPG needs to know which terminal to use for passphrase prompts. Add this to your shell profile (~/.bashrc, ~/.zshrc, or ~/.bash_profile):
export GPG_TTY=
ot a ttyThen reload your shell configuration:
source ~/.bashrc # or ~/.zshrcKill any existing GPG agent processes and let it restart fresh:
gpgconf --kill gpg-agentAlternatively, you can use:
killall gpg-agent && gpg-agent --daemonThen try your Git commit again.
List your GPG keys and check their expiration dates:
gpg --list-keysLook for "expired" next to your key. If expired, you need to extend the expiration or generate a new key:
gpg --edit-key YOUR_KEY_ID
gpg> expire
# Follow prompts to set new expiration
gpg> saveEnsure Git is configured with the correct signing key. First, list your secret keys:
gpg --list-secret-keys --keyid-format=longCopy the key ID (the long hex string after the slash) and configure Git:
git config --global user.signingkey YOUR_KEY_IDAlso check for conflicting local configuration:
git config --local --get user.signingkeyIf this shows a different key, unset it:
git config --local --unset user.signingkeyOn some systems, Git may use the wrong GPG version. Explicitly set the GPG program:
# For systems using gpg2
git config --global gpg.program gpg2
# Or for standard gpg
git config --global gpg.program gpgVerify which gpg is being used:
which gpg
which gpg2On macOS, install pinentry-mac for a graphical passphrase prompt:
brew install pinentry-macConfigure GPG to use it:
echo "pinentry-program " >> ~/.gnupg/gpg-agent.conf
gpgconf --kill gpg-agentFor Apple Silicon Macs, ensure the path is correct (/opt/homebrew/bin/pinentry-mac).
Debugging with GIT_TRACE: For detailed diagnostics, enable Git tracing:
GIT_TRACE=1 git commit -m "test"This shows exactly what commands Git is running and can help identify where the signing process fails.
Multiple GPG Installations: On macOS, you might have GPG from both Homebrew and GPG Suite. These can conflict. Check with which -a gpg and consider uninstalling one.
SSH Agent vs GPG Agent: If you're using GPG for SSH authentication as well, ensure your agents aren't conflicting. The SSH_AUTH_SOCK variable should point to the correct socket.
Headless/CI Environments: In environments without a TTY (like CI/CD), you may need to use a passphrase-less key or configure GPG for unattended operation with --batch and --pinentry-mode loopback options.
WSL Considerations: On Windows Subsystem for Linux, you may need additional configuration to make GPG work properly, including setting up gpg-agent with proper socket locations.
kex_exchange_identification: Connection closed by remote host
Connection closed by remote host when connecting to Git server
fatal: unable to access: Proxy auto-configuration failed
How to fix 'Proxy auto-configuration failed' in Git
fatal: unable to access: Authentication failed (proxy requires basic auth)
How to fix 'Authentication failed (proxy requires basic auth)' in Git
fatal: unable to access: no_proxy configuration not working
How to fix 'no_proxy configuration not working' in Git
fatal: unable to read tree object in treeless clone
How to fix 'unable to read tree object in treeless clone' in Git