Files
aimag-linux/tutorials/Git-GPG.md
T

4.5 KiB

Git with GPG

install

sudo apt install gnupg

Сгенерировать ключи

Опции

  1. RSA & RSA
  2. 4096
  3. реальное имя
  4. адрес почты, используемый на гитхабе
  5. пароль на ключи
artem@artem-tb:~$ gpg --full-generate-key
gpg (GnuPG) 2.4.4; Copyright (C) 2024 g10 Code GmbH
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

gpg: directory '/home/artem/.gnupg' created
gpg: keybox '/home/artem/.gnupg/pubring.kbx' created
Please select what kind of key you want:
   (1) RSA and RSA
   (2) DSA and Elgamal
   (3) DSA (sign only)
   (4) RSA (sign only)
   (9) ECC (sign and encrypt) *default*
  (10) ECC (sign only)
  (14) Existing key from card
Your selection? 1
RSA keys may be between 1024 and 4096 bits long.
What keysize do you want? (3072) 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) 4m
Key expires at Tue 16 Feb 2027 05:20:36 PM MSK
Is this correct? (y/N) y

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

Real name: Artem Trunov
Email address: datamove@gmail.com
Comment: gpgkey1
You selected this USER-ID:
    "Artem Trunov (gpgkey1) <datamove@gmail.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.
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: /home/artem/.gnupg/trustdb.gpg: trustdb created
gpg: directory '/home/artem/.gnupg/openpgp-revocs.d' created
gpg: revocation certificate stored as '/home/artem/.gnupg/openpgp-revocs.d/4307B923877023C087AFB08286EA6415A234F542.rev'
public and secret key created and signed.

pub   rsa4096 2026-10-19 [SC] [expires: 2027-02-16]
      4307B923877023C087AFB08286EA6415A234F542
uid                      Artem Trunov (gpgkey1) <datamove@gmail.com>
sub   rsa4096 2026-10-19 [E] [expires: 2027-02-16]


посмотреть ID ключа

artem@artem-tb:~$ gpg --list-secret-keys --keyid-format=long
gpg: checking the trustdb
gpg: marginals needed: 3  completes needed: 1  trust model: pgp
gpg: depth: 0  valid:   1  signed:   0  trust: 0-, 0q, 0n, 0m, 0f, 1u
gpg: next trustdb check due at 2027-02-16
/home/artem/.gnupg/pubring.kbx
------------------------------
sec   rsa4096/86EA6415A234F542 2026-10-19 [SC] [expires: 2027-02-16]
      4307B923877023C087AFB08286EA6415A234F542
uid                 [ultimate] Artem Trunov (gpgkey1) <datamove@gmail.com>
ssb   rsa4096/C1215132348CE608 2026-10-19 [E] [expires: 2027-02-16]

идентификатор - в стороке, начинающийсяя с sec, e.g. 86EA6415A234F542

Конфигурация git на сервере

git config --global user.signingkey ВАШ ID

опция автоматически подписывать все коммиты

git config --global commit.gpgsign true

Добавление ключа в гит

artem@artem-tb:~$ gpg --armor --export 86EA6415A234F542
-----BEGIN PGP PUBLIC KEY BLOCK-----

mQINBGrWJ4YBEADKiI3o5lTQo6PFgWsNW0IZv81lmMRCMDz7DtmUNAd74QN/FjcA
Yqdwv/bqNzec8dtbSMOZJov029+tUsHR+X4A41KDZWRyJm6FtwYQfH9VL3tI2ru+
...

скопируйте в буфер обмена и вставьте в разделе Settings->SSH and GPG Keys->New GPG Key в вашеv профилt на гитхабе.

Проверяем

сделайте изменение, git теперь должен предложить вам ввести пароль.

После push ваши коммиты видны на сайте как верифицированные:

Git-gpg-verified-commit.png

GPG agent

так как как в случае с SSH вы можете не вводить пароль каждый раз, агент запомнит его!

ps aux | grep gpg-agent
gpgconf --kill gpg-agent
gpg-connect-agent reloadagent /bye