How to fix error: gpg failed to sign the data fatal: failed to write commit object in Git
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.
Quick fix
Fixes error: gpg failed to sign the data fatal: failed to write commit object
echo "test" | gpg --clearsign- Test GPG signing directly
- Set the GPG_TTY environment variable
- Restart the GPG agent
- Check for expired GPG keys
echo "test" | gpg --clearsignerror: gpg failed to sign the data fatal: failed to write commit object
What this error means
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.
Typical symptoms
- Git commits fail with "gpg failed to sign the data" message
- The error "fatal: failed to write commit object" appears after the GPG error
- Git rebase or merge operations fail during commit creation
- CI/CD pipelines fail when attempting to create signed commits
- Commits work only when signing is temporarily disabled
Common causes
- GPG_TTY environment variable is not set, preventing passphrase prompts
- The GPG key has expired and needs to be renewed or replaced
- The gpg-agent process is hung or in a bad state
- Git is configured to use the wrong GPG program (gpg vs gpg2)
- The signing key configured in Git does not match available GPG keys
- On macOS, pinentry-mac is not installed or misconfigured
- Multiple GPG installations are conflicting with each other
- The terminal session cannot display the passphrase prompt
How to fix "error: gpg failed to sign the data fatal: failed to write commit object"
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).
Advanced notes
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.
Frequently asked questions
- What does "error: gpg failed to sign the data fatal: failed to write commit objec" mean?
- 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.
- How do I fix "error: gpg failed to sign the data fatal: failed to write commit objec"?
- 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.
- What causes "error: gpg failed to sign the data fatal: failed to write commit objec"?
- GPG_TTY environment variable is not set, preventing passphrase prompts The GPG key has expired and needs to be renewed or replaced The gpg-agent process is hung or in a bad state
- Is the "error: gpg failed to sign the data fatal: failed to write commit objec" error serious?
- It is a medium-severity error โ more of a blocker than a risk. The steps above resolve it.
Official resources & further reading
- ๐Git Official Documentation
- ๐
- ๐
- ๐
- ๐