Git配置及使用技巧
约 446 字大约 1 分钟
2023-12-06
生成SSH公钥
# 推荐更安全的ED25519算法
ssh-keygen -t ed25519 -C "xxxx"
# 目前ssh-keygen 默认的 rsa算法,一些平台已不支持!
ssh-keygen -t rsa -C "xxxx"
复制SSH公钥到仓库
cat ~/.ssh/id_ed25519.pub
# 或
cat ~/.ssh/id_rsa.pub
将公钥中扥内容复制,配置到git仓库中,不同仓库配置方式略有不同,参考文末质料。
指定仓库认证的公钥
本地生成多种算法公钥后.ssh文件夹中存在多种公钥文件,当我没需要对不同的git仓库配置不同的公钥时可以在~/.ssh文件夹创建config文件,配置参考如下内容。
Host gitee.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_ed25519
Host codeup.aliyun.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa
验证配置是否生效
配置完以上步骤后,在客户端执行如命令
# 验证ssh公钥配置是否生效。
ssh git@xxx.com
# 以阿里codeup为例,执行命令后出现如下输出表示成功。
ssh git@codeup.aliyun.com
# C:\Windows\system32>ssh git@codeup.aliyun.com
# Welcome to Codeup, hzboy192@126.com!
# Connection to codeup.aliyun.com closed.
使用技巧
- git简化add/commit/push命令,并整合到一起。在用户目录.gitconfig文件添加别名acp,如下:
# 添加别名
[alias]
a = add
c = commit
o = checkout
p = pull
acp = "!f() { git add . && git commit -m \"$1\" && git push; }; f"
使用方法:
git a . # 添加所有文件
git c -m "commit message" # 提交
git p # 推送
git acp "commit message" # 提交并推送
参考质料
参考质料:
阿里codeup ssh配置:[如何配置SSH密钥及自定义SSH认证密钥的路径](https://help.aliyun.com/document_detail/153709.html)
gitee ssh配置:[SSH 公钥设置 | Gitee 产品文档](https://help.gitee.com/base/account/SSH公钥设置)