Signed commit Signing git commits can be a life-saver when you have to prove that you (or not) are the author of commits or that a commit was changed by the other person. There are few simple yet not so obvious steps needed to make it work

Generate your public/private key pair

Run gpg:

gpg --full-gen-key # gpg 2.x

or

gpg --gen-key # gpg 1.x
  • Choose RSA and RSA
  • Set keysize to 4096
  • Set validity period of your key (whatever suits you)
  • Enter your details (name and email). It should match your committer identity, otherwise you’ll have to manually inform git which key to use

Export your public key

  • Get you public key by running:
     gpg --list-secret-keys --keyid-format LONG
    

    Find your key and copy the id, it’s preceded by sec 4096R/:

    sec   4096R/123456789ABCDEF0
    

    Then run export command:

    gpg --armor --export 123456789ABCDEF0
    

    and copy the output with your public key (along with -----BEGIN PGP PUBLIC KEY BLOCK----- and -----END PGP PUBLIC KEY BLOCK-----).

Add a public key to git repository

Github

Go to your profile settings, to SSH and GPG keys section and click on New GPG key where you can paste and save your key.

Gitlab

Go to user settings, to GPG keys, paste your key and press Add key

Setup your local repository and tools

Git repositories are aware about your keys now but we need to instruct git to use your key

Forcing which public key use to sign

In case of a mismatch between your git user email and email used for gpg keys, you can be explicit about the key used for signging:

git config --global user.signingkey 123456789ABCDEF0

Configure your IDE

In case committing with IDE like IntelliJ you have to configure the gpg to not use the terminal but rather it’s own GUI. Just add the following line to ~/.gnupg/gpg.conf file:

no-tty

On the other hand it will lead to problems with committing using a ssh session, thus be aware that you have to switch on/off this option depending on whether or not you’re working remotely.

Sign commit

We can do it both ways:

  • Add -S switch each time to sign a commit:
    git commit -S -m add new feature
    
  • Configure git to use signing as the default option:
    git config --global commit.gpgsign true
    

    In case of a linux distribution using gpg2 you have to set it explicitly:

    git config --global gpg.program gpg2
    

Checking signatures

You can easily see which commits are signed and verified by using gitlab and github repository pages. Every signed commit will have a nice green Verified label. The other option to check it is to use git log command:

git log --show-signature

After each signed commit you’ll see info from gpg about the status of the given commits (is it verified or not):

Signed commit in log

More info:

Updated:

Leave a comment