提交Git代码时提示“cannot run gpg: no such file or directory”求助
Hey there, fellow Git newbie! I’ve been right where you are—stuck on a random GPG error when trying to commit via Terminal, with zero interest in messing with graphical tools. Let’s get this sorted.
What’s causing this?
Git is trying to sign your commit with a GPG key (a security feature to verify your commits are legit), but either:
- You don’t have GPG installed on your Mac, or
- Git can’t find the GPG executable in its system path.
Solution 1: Turn off commit signing (quick, no-fuss fix)
If you don’t need signed commits (totally fine for personal projects or just getting started), you can disable this setting globally or just for your current repo:
- Global disable (applies to all your Git repositories):
git config --global commit.gpgsign false - Per-repository disable (only affects the repo you’re currently working in):
git config commit.gpgsign false
Run either command, then try your commit again—it should work immediately.
Solution 2: Install GPG and set up signed commits (more secure)
If you want to keep using signed commits (they’re great for proving commits actually come from you), here’s how to set everything up entirely via Terminal:
- Install GPG using Homebrew:
If you don’t have Homebrew installed yet, run this standard Terminal command first (no browser or graphical steps needed):
Then install GPG:/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"brew install gnupg - Tell Git where to find GPG:
git config --global gpg.program gpg - Generate a GPG key:
Follow the on-screen prompts—stick with RSA encryption, use a 4096-bit key length, and enter the name/email linked to your GitLab account:gpg --full-generate-key - Find your GPG key ID:
Run this command, then look for the line starting withsec—the string after/is your unique key ID (e.g.,ABC123DEF456):gpg --list-secret-keys --keyid-format LONG - Configure Git to use this key for signing:
ReplaceABC123DEF456with your actual key ID:git config --global user.signingkey ABC123DEF456 - Test it out:
Make a tiny change to a file, stage it, then commit—you shouldn’t see the GPG error anymore:git add . git commit -m "Test signed commit"
Troubleshooting tip
If you installed GPG but Git still can’t find it, check where GPG is located on your system:
which gpg
Take the path it outputs (e.g., /usr/local/bin/gpg) and update your Git config to use that exact path:
git config --global gpg.program /usr/local/bin/gpg
内容的提问来源于stack exchange,提问作者user9790132

