Signing commits with GPG on macOS Guidelines
#gpg#macos#git
Published at - Install GPG with Homebrew:
brew install gpg2 gnupg pinentry-macWith pinentry-mac you will be able to enter your password in a popup window instead of the terminal.
- Create the .gnupg directory:
mkdir ~/.gnupg- Use pinentry-mac:
echo "pinentry-program $(brew --prefix)/bin/pinentry-mac" > ~/.gnupg/gpg-agent.conf- Update or create the ~/.gnupg/gpg.conf file:
echo "use-agent" >> ~/.gnupg/gpg.conf- Modify your shell
Append the following to your ~/.bash_profile or ~/.bashrc or ~/.zshrc
For instance
echo "export GPG_TTY=$(tty)" >> ~/.zshrc- Restart your shell
# on the built-in bash on macos usesource ~/.bash_profile# if using bash through homebrew over ssh usesource ~/.bashrc# and if using zshsource ~/.zshrc- Update permission
chmod 700 ~/.gnupg/*- Kill the gpg-agent
killall gpg-agent- Create your GPG key with pinentry-mac
gpg --full-gen-key --pinentry-mode loopback- Answer the questions
Please select what kind of key you want: (1) RSA and RSA (default) (2) DSA and Elgamal (3) DSA (sign only) (4) RSA (sign only)Your selection? 4RSA keys may be between 1024 and 4096 bits long.What keysize do you want? (2048) 4096Requested keysize is 4096 bitsPlease specify how long the key should be valid. 0 = key does not expire <n> = key expires in n days <n>w = key expires in n weeks <n>m = key expires in n months <n>y = key expires in n yearsKey is valid for? (0) 0Key does not expire at allIs this correct? (y/N) y
You need a user ID to identify your key; the software constructs the user IDfrom the Real Name, Comment and Email Address in this form: "Su Ho (S) <[email protected]>"
Real name: Su HoComment:You selected this USER-ID: "Su Ho <[email protected]>"
Change (N)ame, (C)omment, (E)mail or (O)kay/(Q)uit? oYou need a Passphrase to protect your secret key.- Get your key ID
gpg --list-secret-keys --keyid-format SHORTThis command will generate an output similar to this:
/Users/suho/.gnupg/pubring.kbx------------------------------sec ed25519/XXXXXXXX 2023-11-29 [SC]...You will need to copy the key ID, in this case XXXXXXXX.
- Export the fingerprint
gpg --armor --export XXXXXXXX- Configure Git to use gpg and sign all commits
git config --global user.signingkey XXXXXXXXgit config --global commit.gpgsign true- Perform a test commit
git commit -S -s -m "Signed Commit" --allow-empty- Pinentry Prompt
You should see a popup window asking for your password. Enter your password and click OK.
- Submit your GPG key to GitHub
gpg --armor --export XXXXXXXX | pbcopyThen login into github.com and go to your settings, SSH and GPG Keys, and add your GPG key from the page.
Follow this guide for more details.