!!! For M1/M2 apple silicon see this comment:
For MacOS | Mojave | High Sierra
brew upgrade gnupg
brew install pinentry-mac
echo "pinentry-program /usr/local/bin/pinentry-mac" >> ~/.gnupg/gpg-agent.conf
killall gpg-agent && gpg-agent --daemon
git config --global gpg.program gpg
git config --global commit.gpgsign true
Thanks, I use @shaunsim15 ways, it doesn't need to add
GIT_TRACE=1 git commit -a -m 'signing test'
for me, but to executekillall gpg-agent && gpg-agent --daemon
at last.