Git SSH 配置

· Technology

由于作者本人长期使用GitHub,故以此为示例。Gitea、Gitee、GitLab同理。

基本 Git 信息配置

首先我们需要配置Git的基本信息。

git config --global user.name "AH-dark" # 对应 GitHub Username
git config --global user.email "ahdark@outlook.com" # 对应 GiHub Email

需要注意的是,我们以前所使用的使用账号密码登录 GitHub SSH 的方法已经不被允许。

同时,我建议每个人都为自己的GitHub账户配置2FA身份验证,同时尽量避免使用个人域名邮箱注册GitHub。当你的域名过期或者被弃用,他人只需购买你的域名并进行相关配置即可轻松盗取你的账号。

SSH 公私钥生成

Windows 安装 Git 所带的 Git Bash 或 Linux Terminal 都可以进行此处的配置。

ssh-keygen -t rsa -C "ahdark@outlook.com"

此后连按三次 Enter 键,使用默认路径即可。

例如,我的SSH公钥生成在 C:\Users\ahdark\.ssh\id_rsa.pub ,私钥在 C:\Users\ahdark\.ssh\id_rsa

将私钥添加到 SSH Agent

# 后台启动 SSH Agent
eval $(ssh-agent -s)

# 添加私钥
ssh-add /c/Users/ahdark/.ssh/id_rsa

将公钥添加到 GitHub

  1. 前往 GitHub Settings - SSH and GPG keys
  2. 点击 New SSH Key 添加新的 SSH key
  3. 前往公钥所在路径,将其内容复制到输入框中。
  4. 取一个适宜的名字,保存
我的 SSH Key
我的 SSH Key

使用 SSH Clone 仓库

Image
git clone git@github.com:AlphaPic-Storage/frontend.git alphapic-frontend

补充:GPG 密钥提高安全性

可参考之前的文章:Git GnuPG 配置教程

Comments

Send Comments

Markdown supported. Please keep comments clean.