Git

use GPG key on git

Posted on Updated on

Introduction

Here is the way to use GPG key on git

 

Procedure

 

1. Create GPG key

gpg --full-generate-key

Recommend Setting:

Field Value
Key type RSA (1)
Key length 4096
Expire Date 0
Real Name Your Name in .gitconfig
Email Your Email in .gitconfig & GitHub verified email
Comment github

 

2. Check your GPG key information

# List selected key
gpg --list-ley KEY_ID
# List all key with long format
gpg --list-keys --keyid-format LONG

 

3. Export the Public key to

Copy the output to your github account

gpg --armor --export KEY_ID

 

4. Edit your .gitconfig

Command:

git config --global --edit

Insert the following information

[user]
  name = YOUR_NAME               # Should be same as your commit name
  email = YOUR_EMAIL             # Should be same as your GitHub verification email and GPG uid email
  signingkey = YOUR_SIGNING_KEY  # your KEY_ID that you want to use
[gpg]
  program = GPG_BINARY_PATH      # usually in "/usr/local/bin/gpg"
[commit]
  gpgsign = true

 

5. Setting on your shell

Insert this line to your shell

export GPG_TTY=$(tty)

 

Useful command

 

Edit the existing GPG key

gpg --edit-key KEY_ID

gpg> adduid   #Add a new uid on your key
gpg> uid 1    #Select the uid 1
gpg> revuid   #Revoke the selected uid from the key
gpg> save.    #Save the modification

 

Test you GPG key

You may run this command first before using github desktop to commit anythings. Since github desktop CANNOT pop-up the GPG key passphase input dialog to the user, you need this command to unlock the GPG key first. After that the github deskop can package you commit with your GPG key without the passphase.

echo "test" | gpg --clearsign

 

Reference

https://help.github.com/en/github/authenticating-to-github/generating-a-new-gpg-key
https://help.github.com/en/github/authenticating-to-github/telling-git-about-your-signing-key#telling-git-about-your-gpg-key-1

GPG signing – git github-desktop

Here is a short guide that will help you setup your environment to create signed commits or signed tags with Git locally. This has been extensively tested on Windows with Git and the Github Desktop application: I use it every day for my professional development projects.

I you face any issue, feel free to leave a comment below.

Summary

  1. Sign commits or tags
  2. Key passphrase
  3. Disable signatures
  4. Renew a GPG key

Sign commits or tags

  1. Generate a GPG key and add it to Github: https://help.github.com/articles/generating-a-new-gpg-key
    (if you don’t want to type a passphrase on every commit, you need to press “Enter” when the console will prompt you to type a passphrase)

  2. Open the .gitconfig configuration file by typing git config --global --edit in a terminal (since this file can exists in different places depending on your operating system, the command line will prompt git binary and open your default editor)

  3. Configure Git by replacing GITHUB_EMAIL, SIGNING_KEY and GPG_BINARY_PATH with your own data:

[user]
  name = Xavier Foucrier
  email = GITHUB_EMAIL
  signingkey = SIGNING_KEY
[gpg]
  program = GPG_BINARY_PATH
[commit]
  gpgsign = true
[tag]
  gpgsign = true
  • GITHUB_EMAIL: the email address used to login on Github

  • SIGNING_KEY: the GPG key identifier used to sign commits (should follow the GPG key ID convention, like this example: https://help.github.com/articles/telling-git-about-your-signing-key/#telling-git-about-your-gpg-key-1)

  • GPG_BINARY_PATH: the GPG binary file path depending on your Git install and your operating system:

    • Windows: gpg, gpg.exe or C:\\Program Files\\Git\\usr\\bin\\gpg.exe
      (can be found using where gpg in a terminal)

      Some system may contain multiple gpg binaries, in this case you can execute the following command line with PowerShell to use the more appropriate one: git config --global gpg.program $(Resolve-Path (Get-Command gpg | Select-Object -Expand Source) | Select-Object -Expand Path)

    • Mac or Linux: gpg or /usr/local/bin/gpg
      (can be found using which gpg in a terminal)

  1. Enjoy signed commits with your favorite code editor, Github Desktop application, and even command line using git commit -S -m "Commit message" 🎉

Key passphrase

In order for GPG to automatically store your key passphrase (even empty), so you don’t have to enter it every time you sign a commit, Github recommend using the following tools:

This is necessary to let GPG launch the gpg-agent as a system daemon when signing commits.

Disable signatures

If you want to temporarily pause GPG signatures for your commits or tags, just set gpgsign = false in your .gitconfig configuration file with git config --global commit.gpgsign false (for commits) or git config --global tag.gpgsign false (for tags).

Renew a GPG key

If the key you have defined in the .gitconfig configuration file has expired, you can’t sign commits anymore. You can easily renew it by following these steps:

  1. list the secrets keys with gpg --list-secret-keys

  2. edit the key you want to renew with gpg --edit-key SIGNING_KEY
    (the GPG key used to sign commits, as defined in your .gitconfig configuration file)

  3. gpg prompt is ready: you should see gpg>

  4. type expire to select a new expiration delay and confirm

  5. type trust to trust the selected key as “ultimate”
    (this step is not needed if your current key is already trusted as an “ultimate” key)

  6. type quit and confirm you wish to save changes

  7. enjoy a fresh renewed GPG key!

Thanks everyone for reading! 👀

view raw

gpg-signing.md

hosted with ❤ by GitHub

https://stackoverflow.com/questions/51504367/gpg-agent-forwarding-inappropriate-ioctl-for-device


For troubleshooting, two things to first try:
run `git config –global gpg.program gpg2`, to make sure git uses gpg2 and not gpg
run `echo "test" | gpg2 –clearsign`, to make sure gpg2 itself is working
If that all looks all right, one next thing to try:
run `brew install pinentry` to ensure you have a good tool installed for passphrase entry
If after that install and you re-try git commit and still get the "failed to sign the data" error:
run `gpgconf –kill gpg-agent` to kill any running agent that might be hung
If that says gpgconf isn’t installed or doesn’t have a `–kill` option, you might try this:
`cp ~/.gnupg ~/.gnupg-GOOD` to save a copy of your `~/.gnupg` to revert to later if needed
`brew install gnupg21` to install `GnuPG 2.1`
The reason for saving a copy of your `~/.gnupg` dir is that GnuPG 2.1 potentially creates/changes some key data in way that isn’t backward-compatible with GnuPG 2.0 and earlier, so if you want to go back later, you can do `mv ~/.gnupg ~/.gnupg21 && mv ~/.gnupg-GOOD ~/.gnupg`.
Otherwise there are some basic steps to run to check you’ve got a working GnuPG environment:
run `gpg2 -K –keyid-format SHORT`, to check that you have at least one key pair
If the output of that shows you have no secret key for GnuPG to use, then you need to create one:
run `gpg2 –gen-key`, to have GnuPG walk you through the steps for creating a key pair
source: https://stackoverflow.com/a/41054093/4902448

view raw

gpg_fix.txt

hosted with ❤ by GitHub

Procedure after enabling “Keep my email addresses private” on github

Posted on Updated on

Introduction

After enabling the “Keep my email addresses private” on GitHub, your old commit on each repo still retains your old commit email. You may want to modify all of the commit emails to your GitHub “no-reply” email. You may use the following method.

You can insert “.patch” at the end of the commit weblink to view the raw information of your commit. Here is the Example.

 

Procedure

 

1. Download the git repo in bare mode

git clone --bare https://github.com/user/REPO.git
cd REPO.git

 

2. Create the change_email.sh.sh file

vim change_email.sh

Copy the following lines, modify the OLD_EMAIL, CORRECT_NAME, CORRECT_EMAIL fields to your old email of git, git name and new email that you want to modify

#!/bin/sh

git filter-branch --env-filter '

OLD_EMAIL="your-old-email@example.com"
CORRECT_NAME="Your Correct Name"
CORRECT_EMAIL="your-correct-email@example.com"

if [ "$GIT_COMMITTER_EMAIL" = "$OLD_EMAIL" ]
then
    export GIT_COMMITTER_NAME="$CORRECT_NAME"
    export GIT_COMMITTER_EMAIL="$CORRECT_EMAIL"
fi
if [ "$GIT_AUTHOR_EMAIL" = "$OLD_EMAIL" ]
then
    export GIT_AUTHOR_NAME="$CORRECT_NAME"
    export GIT_AUTHOR_EMAIL="$CORRECT_EMAIL"
fi
' --tag-name-filter cat -- --branches --tags

 

3. Run the change_email.sh file

sh ./change_email.sh

 

4. Review the changes

Check the commit email whether the fix is applied.

git log

 

5. Upload the change to the git server

git push --force --tags origin 'refs/heads/*'

 

Reference

https://help.github.com/en/github/using-git/changing-author-info