使用 GPG 为极狐GitLab git commit 签名

本文介绍了如何使用GPG为GitLab的git commit签名,包括生成GPG keypair、在GitLab中添加public key、配置Git、进行commit以及验证签名的过程,确保代码提交的安全性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目录

git commit 签名

GPG key 生成

使用 GPG 为极狐GitLab git commit 签名

在极狐GitLab 中添加 GPG public key

配置 Git

进行 Git 提交

查看签名信息


git commit 签名

git commit 签名是对 git 的 commit 信息进行一个验证,确保代码提交者是代码修改者本身,防止恶意提交,保护代码的安全。git 有自身的扩展,用来对 commit 进行签名,比如使用 gpg 即可完成 commit 签名。

GPG key 生成

执行 gpg --full-generate-key 即可生成一对 gpg key pair:

gpg --full-generate-key
gpg (GnuPG) 2.2.19; Copyright (C) 2019 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)
  (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) 1y
Key expires at Wed Jun 28 21:45:26 2023 CST
Is this correct? (y/N) y

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

Real name: xiaomage
Email address: devops008@sina.com
Comment: gpg signature git commit
You selected this USER-ID:
    "xiaomage (gpg signature git commit) <devops008@sina.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: key C9DE119E4E550644 marked as ultimately trusted
gpg: directory '/root/.gnupg/openpgp-revocs.d' created
gpg: revocation certificate stored as '/root/.gnupg/openpgp-revocs.d/9D6DA2C807767AD9ECB335AEC9DE119E4E550644.rev'
public and
### 配置和使用 Git Commit 签名 为了确保每次提交都带有签名,在 Git 中可以通过 GPG (GNU Privacy Guard) 来实现这一功能。GPG 是一种加密技术,允许创建数字签名来验证身份。 #### 设置 GPG 密钥 首先需要生成一个新的 GPG 密钥或者使用现有的密钥: ```bash gpg --full-generate-key ``` 这会启动一系列提示以帮助完成新密钥的创建过程。选择默认选项通常就足够了。一旦有了可用的 GPG 密钥,则可通过以下命令查看其列表以及对应的指纹(Fingerprint): ```bash gpg --list-secret-keys --keyid-format LONG ``` 找到要使用的私钥后,复制该条目的整个指纹字符串以便稍后配置 Git 使用它。 #### 将 GPG 密钥添加至 GitHub 或其他远程仓库服务提供商账户 如果打算让托管平台(如GitHub)能够识别这些签名,则还需要按照对应网站上的说明上传公钥部分。对于 GitHub 用户来说,可以在 Settings -> SSH and GPG keys 页面下操作。 #### 在 Git 中启用提交签名 全局范围内开启自动签署所有提交的功能: ```bash git config --global commit.gpgsign true ``` 指定特定邮箱地址关联某个具体的 GPG 密钥(如果有多个的话),通过下面的方式设定: ```bash git config --global user.signingkey YOUR_GPG_KEY_ID ``` 这里 `YOUR_GPG_KEY_ID` 应替换为你之前记录下来的那串字符中的任意一部分作为标识符即可。 当以上步骤完成后,默认情况下所有的提交都会被加上 `-S` 参数从而触发 GPG 签名机制[^1]。 对于已经存在的未签名的历史提交,可以采用交互式变基的方法批量追加签名而不改变原始的时间戳信息[^3]: ```bash git rebase --exec 'GIT_COMMITTER_DATE="$(git log -1 --format=%at)" git commit --amend --no-edit -n -S' -i HEAD~n ``` 这里的 `HEAD~n` 表示最近 n 次提交;可以根据实际情况调整这个数值大小。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值