DEV Community

Cover image for Sign your git commits with tortoise git on windows
Julian
Julian

Posted on • Updated on

Sign your git commits with tortoise git on windows

after reading this nice article from agrinman about spoofing a git commit, i decided i will have a look on how to sign a git commit.

it is a little bit more complicated than i thought but its still an quite easy task.

first generate a keypair (the --allow-freeform-uid is to overcome the Name must be at least 5 characters long error):

λ gpg --full-generate-key --allow-freeform-uid
gpg (GnuPG) 2.2.8; Copyright (C) 2018 Free Software Foundation, Inc.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

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? 1
RSA keys may be between 1024 and 4096 bits long.
What keysize do you want? (2048) 4096
Requested keysize is 4096 bits
Please 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 years
Key is valid for? (0) 5y
Key expires at 08/09/23 18:32:52 W. Europe Daylight Time
Is this correct? (y/N) y

GnuPG needs to construct a user ID to identify your key.

Real name: devto-example
Email address: devto@example.com
Comment:
You selected this USER-ID:
    "devto-example <devto@example.com>"

Change (N)ame, (C)omment, (E)mail or (O)kay/(Q)uit? o
We need to generate a lot of random bytes. It is a good idea to perform
some other action (type on the keyboard, move the mouse, utilize the
disks) during the prime generation; this gives the random number
generator a better chance to gain enough entropy.
gpg: key 8493DA39DD68724D marked as ultimately trusted
gpg: revocation certificate stored as '<REMOVED>'
public and secret key created and signed.

pub   rsa4096 2018-08-10 [SC] [expires: 2023-08-09]
      F0FB9F40A82A7F2502B652348493DA39DD68724D
uid                      devto-example <devto@example.com>
sub   rsa4096 2018-08-10 [E] [expires: 2023-08-09]
Enter fullscreen mode Exit fullscreen mode

export your public key

λ gpg --armor --export devto@example.com
-----BEGIN PGP PUBLIC KEY BLOCK-----

mQINBFttvksBEADGlRUxbEWnkQTuo3hCQv/Rw41a4A3/T3FZKaIXxO6c+uRn/XN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HpNF/v3f+w0ArOlULktoiCXd5TAVrR2FaHSoKYr49FNrAPehWpYJs8/
mGW6xIcAEQEAAYkCPAQYAQgAJhYhBPD7n0CoKn8lArZSNIST2jndaHJNBQJbbb5L
AhsMBQkJZgGAAAoJEIST2jndaHJNaFMP/j5okfhHVl0GfrUoe/oBxNMNGVDTGhYM
p8YRVAxbGudckREfxg6T6sBQYU3gPQUywer5CgcJ6UGYusIEUxnf5G9hYpAPILMK
PZp1eYB1Om5d2yiikaZGxotNXg8A8vdxhutmUmjpAaAso0RsPzjRaBM/25Osiwkb
6bHBscarEo0ZQQrmgcNJkxzzaE8pX53zF4VKbNlsjtqniyRYn5zeHyTTUaUH0ZLd
6vhLiD/ozZnTnd9eLr1s87h6zv+uj/2xCZlMAebzPld8NFfY+Kr1XDjZA5cwACEW
Pg9oWMA0oE60W7HisWWik4qu3Ul+Nw/La8Onkb5aEWTp7/mq1R1SJdGYi9xjFHFK
IUnvMFcfaSaDZTNh5QykbqpkHWSBC8uQSEMgH+wJDrVq0nJk+hU5EjvAcCKWR/w2
MoS3rhQk3zazsL4KVp5x5Wihw9VkORJdohs7nHXWVAuK8/Z1TZXIeVpxsK9O2S0K
l4PxbEJaOudKEgvp3asmZd1U1BIZje1Adj4TsyFecVqqboUuzsXop1xBjXiN1h8w
DbT+fnc/m1KUJt5tp+DKJh0SsYOPfToU+SZ3E2Zu+8ZDZOq9JmLDEgYaLDiCn+5M
7oXFeWOYtFP07OJBxktI9A+oOUaN2Z71IviLAQcu81xuFZN1oS7jQgaqr6MbjsG9
1UOjABr0Qt9w
=sdjc
-----END PGP PUBLIC KEY BLOCK-----
Enter fullscreen mode Exit fullscreen mode

and add it to https://gitlab.com/profile/gpg_keys and https://github.com/settings/gpg/new

then you have to configure git to use the key by adding the key id to the git config.

to get the key id run:

λ gpg --list-keys --keyid-format LONG devto@example.com
pub   rsa4096/8493DA39DD68724D 2018-08-10 [SC] [expires: 2023-08-09]
      F0FB9F40A82A7F2502B652348493DA39DD68724D
uid                 [ultimate] devto-example <devto@example.com>
sub   rsa4096/75928CD4BDEAE0D7 2018-08-10 [E] [expires: 2023-08-09]
Enter fullscreen mode Exit fullscreen mode

the sort version of the id is the part after pub rsa4096/ -> 8493DA39DD68724D and the long version is in the 2nd line F0FB9F40A82A7F2502B652348493DA39DD68724D because of key collisions i would recommend that you use the long key format.

now add/merge the following to your (global) .gitconfig

[user]
    signingkey = F0FB9F40A82A7F2502B652348493DA39DD68724D

[commit]
    gpgsign = true
[gpg]
    program = "C:/Program Files (x86)/GNU/GnuPG/gpg2.exe"

Enter fullscreen mode Exit fullscreen mode

you need to have gpg installed and adapt the program path according your install path. the signing key is your long key id. to auto sign your commits add commit.gpgsign=true.

now if you do a commit, the commit will be signed. you can easily verify that:

λ git verify-commit HEAD
gpg: Signature made 08/10/18 19:37:21 W. Europe Daylight Time
gpg:                using RSA key F0FB9F40A82A7F2502B652348493DA39DD68724D
gpg: Good signature from "devto-example<devto@example.com>" [ultimate]
Enter fullscreen mode Exit fullscreen mode

or

git log --show-signature
Enter fullscreen mode Exit fullscreen mode

Top comments (0)