赞
踩
git github gitlub
使用ssh克隆代码,使用gpg签名提交代码。
一对公钥和密钥,公钥复制到gitlub上。步骤如下
(1)配置用户名和邮箱
- git config --global user.name "moxun"
- git config --global user.email "xxx@xx.com"
(2) 生成公私钥
两种加密方式:ed25519(推荐)和rsa
ssh-keygen -t ed25519 -C "xxx@xx.com"
ssh-keygen -t rsa -C "xxx@xx.com"
按三次回车enter键即可生成,这里一般不需要添加名称和密码
(3)打开文件查看,路径为C:\Users\用户名\.ssh
上面第一步会生成两个加密文件,如我使用的是ed25519,则id_ed25519文件是私钥,id_ed25519.pub是公钥,打开,全选复制到gitlub上即可。
- git config --global gpg.format ssh
- #git config --global user.signingkey ~/.ssh/id_ed25519.pub
- git config --global user.signingkey ~/.ssh/你的文件名称
-
- git commit -S -m "My commit msg"
配置git提交为gpg签名,提交代码后gitlub上将显示verifiled。
这是个可视化工具,下载后一步步安装即可(建议安装到默认目录即可)。
cmd命令行输入gpg --version,执行成功则说明安装成功。
cmd命令行(或者idea终端或者git bash命令行)输入:
gpg --full-gen-key
弹出框一步步输入即可:
(1)
- $ gpg --full-gen-key
- gpg (GnuPG) 2.4.4-unknown; 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.
-
- 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
(2)
- RSA keys may be between 1024 and 4096 bits long.
- What keysize do you want? (3072)
输入4096
(3)
- 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)
- Key does not expire at all
- Is this correct? (y/N) y
(4)输入名称和邮箱
(5)
- GnuPG needs to construct a user ID to identify your key.
-
- Real name: Zoomie Developer
- Email address: zoomie.developer@zoom.us
- You selected this USER-ID:
- "Zoomie <zoomie.developer@zoom.us>"
-
- Change (N)ame, (C)omment, (E)mail or (O)kay/(Q)uit?
输入O确认。弹框输入密码,可输入也可以不输入。完毕
(1)方法1
- gpg --list-secret-keys --keyid-format LONG <EMAIL>
- #将 <EMAIL> 替换为生成密钥时使用的电子邮件地址:如:gpg --list-secret-keys --keyid-format LONG wtyy@qq.com
在输出中,/后面的即为私钥,如我输出如下,则私钥为5FEC960A78DF29F6,
- sec rsa4096/5FEC960A78DF29F6 2024-04-19 [SC]
- 2C8974FE36C8386038AD3F4B5FEC960A78DF29F6
- uid [ultimate] tina.zhang <tina.zhang@zoom.us>
- ssb rsa4096/371D4A944D473731 2024-04-19 [E]
(2)方法2
打开第一步下载的gpgwin可视化工具也可以查看到
gpg --armor --export 你的密钥
复制输出的公钥,包括 BEGIN PGP PUBLIC KEY BLOCK 和 END PGP PUBLIC KEY BLOCK 行,粘贴到gitlub的gpg设置中即可。
git config --global commit.gpgsign true
settings-->git-->configure GPG key,点击会自动弹出来密钥,确认即可。
往往还需要配置git的gpg路径:
- git config --global gpg.program "C:\Program Files (x86)\GnuPG\bin\gpg.exe"
- #具体路径参考第一步安装的路径
否则commit或者push会报错:
gpg: skipped "88F1EA372F568EBA": No secret key
gpg: signing failed: No secret key
error: gpg failed to sign the data
fatal: failed to write commit object
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。