How to fix error: gpg failed to sign the data fatal: failed to write commit object in Git

GitINTERMEDIATEMEDIUMUpdated Dec 6, 2025ยท 179 views

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
  1. Test GPG signing directly
  2. Set the GPG_TTY environment variable
  3. Restart the GPG agent
  4. Check for expired GPG keys
git โ€” error
echo "test" | gpg --clearsign
error: 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

  1. GPG_TTY environment variable is not set, preventing passphrase prompts
  2. The GPG key has expired and needs to be renewed or replaced
  3. The gpg-agent process is hung or in a bad state
  4. Git is configured to use the wrong GPG program (gpg vs gpg2)
  5. The signing key configured in Git does not match available GPG keys
  6. On macOS, pinentry-mac is not installed or misconfigured
  7. Multiple GPG installations are conflicting with each other
  8. The terminal session cannot display the passphrase prompt

How to fix "error: gpg failed to sign the data fatal: failed to write commit object"

1Test GPG signing directly

First, verify that GPG can sign data outside of Git:

bash
echo "test" | gpg --clearsign

If this fails, the problem is with GPG itself, not Git. If it succeeds and shows a PGP signature block, proceed to check Git configuration.

2Set the GPG_TTY environment variable

GPG needs to know which terminal to use for passphrase prompts. Add this to your shell profile (~/.bashrc, ~/.zshrc, or ~/.bash_profile):

bash
export GPG_TTY=
ot a tty

Then reload your shell configuration:

bash
source ~/.bashrc  # or ~/.zshrc
3Restart the GPG agent

Kill any existing GPG agent processes and let it restart fresh:

bash
gpgconf --kill gpg-agent

Alternatively, you can use:

bash
killall gpg-agent && gpg-agent --daemon

Then try your Git commit again.

4Check for expired GPG keys

List your GPG keys and check their expiration dates:

bash
gpg --list-keys

Look for "expired" next to your key. If expired, you need to extend the expiration or generate a new key:

bash
gpg --edit-key YOUR_KEY_ID
gpg> expire
# Follow prompts to set new expiration
gpg> save
5Verify Git signing key configuration

Ensure Git is configured with the correct signing key. First, list your secret keys:

bash
gpg --list-secret-keys --keyid-format=long

Copy the key ID (the long hex string after the slash) and configure Git:

bash
git config --global user.signingkey YOUR_KEY_ID

Also check for conflicting local configuration:

bash
git config --local --get user.signingkey

If this shows a different key, unset it:

bash
git config --local --unset user.signingkey
6Configure the correct GPG program

On some systems, Git may use the wrong GPG version. Explicitly set the GPG program:

bash
# For systems using gpg2
git config --global gpg.program gpg2

# Or for standard gpg
git config --global gpg.program gpg

Verify which gpg is being used:

bash
which gpg
which gpg2
7macOS: Install and configure pinentry-mac

On macOS, install pinentry-mac for a graphical passphrase prompt:

bash
brew install pinentry-mac

Configure GPG to use it:

bash
echo "pinentry-program " >> ~/.gnupg/gpg-agent.conf
gpgconf --kill gpg-agent

For 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:

bash
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