设置一个自定义名称的密钥,用于 git 仓库上下传使用
在 Windows 上为 Git 仓库设置自定义名称的 SSH 密钥教程
1. 生成自定义名称的 SSH 密钥
打开 Git Bash(在开始菜单中搜索或在任意文件夹内右键选择)。
执行以下命令,并将 your_email@example.com 替换为你的邮箱地址,custom_key_name 替换为你想要的自定义密钥文件名。
codeBash
ssh-keygen -t rsa -b 4096 -C "your_email@example.com" -f ~/.ssh/custom_key_name2. 将新的 SSH 密钥添加到 SSH-Agent
首先,启动 SSH-Agent。
codeBash
eval $(ssh-agent -s)然后,添加你刚刚生成的私钥。
codeBash
ssh-add ~/.ssh/custom_key_name3. 配置 SSH 以使用新的密钥
在 ~/.ssh/ 目录下创建一个名为 config 的文件(如果不存在的话)。
打开 config 文件并添加以下内容。将 custom_host 替换为一个自定义别名(例如 github-custom),将 HostName 替换为你的 Git 仓库托管服务商的域名(例如 github.com),并将 IdentityFile 的路径指向你的私钥文件。
codeCode
Host custom_hostHostName github.comUser gitIdentityFile ~/.ssh/custom_key_name4. 将公钥添加到你的 Git 仓库托管平台
复制你的公钥内容。
codeBash
clip < ~/.ssh/custom_key_name.pub登录到你的 Git 仓库托管平台(如 GitHub、GitLab 等),进入 "Settings" -> "SSH and GPG keys",点击 "New SSH key",将复制的公钥粘贴进去并保存。
5. 配置本地 Git 仓库使用新的密钥
对于一个新的仓库,使用你自定义的 Host 来克隆。
codeBash
git clone git@custom_host:USERNAME/REPOSITORY.git对于一个已存在的仓库,更新其远程仓库 URL。
codeBash
git remote set-url origin git@custom_host:USERNAME/REPOSITORY.git