主题模式
Git 身份与 SSH:配置并验证实际生效值
user.name 与 user.email 写入提交记录,SSH 密钥用于访问服务器;它们分别配置。这次只给 .cache/git-pnpm-walkthrough/repo 设置本地身份,再用真实提交检查作者。
查看git配置信息
在常用命令教程创建的 repo 中执行:
powershell
git config --local --show-origin --get-regexp '^user\.'本次设置身份后的输出:
text
file:.git/config user.name Docs Demo
file:.git/config user.email [email protected]file:.git/config 表明来自当前示例仓库。去掉 --local,即 git config --show-origin --get-regexp '^user\.',可检查其他作用域;分享诊断输出前检查私人信息。
分别检查身份与认证配置
powershell
git config --local --get user.name
git config --local --get user.email
git var GIT_AUTHOR_IDENT前两条分别输出 Docs Demo 和 [email protected];第三条的身份部分也为 Docs Demo <[email protected]>,后面跟时间与时区。
git config --get credential.helper 检查凭据助手。Git 没有通用的 user.password 登录设置;HTTPS 凭据应由受信任的凭据管理器处理,邮箱不正确也不会直接导致 SSH 登录失败。
设置git用户名、邮箱的配置
powershell
git config --local user.name "Docs Demo"
git config --local user.email "[email protected]"示例邮箱使用保留域名,不代表托管平台账号。创建提交后执行:
powershell
git log -1 --format="%h %s | %an <%ae>"首次提交实际输出:
text
c5c94c1 docs: add readme | Docs Demo <[email protected]>这验证了该提交写入的作者。修改配置不会追改旧提交。个人机器确需设置所有仓库的默认身份时,语法仍是 git config --global user.name "Your Name"、git config --global user.email "[email protected]";练习不需要这两个全局写入命令。
使用 SSH 的方式
需要 SSH 访问托管平台时,再创建密钥:
bash
ssh-keygen -t ed25519 -C "[email protected]"已有同名密钥时选新文件名,别覆盖。把 .pub 公钥添加到托管平台账号,私钥只留本机。以 GitHub 为例:ssh -T [email protected];首次连接先对照官方主机指纹,成功提示中应显示你的账号。Permission denied (publickey) 时检查公钥账号、密钥路径和 ssh-agent。
本次的 origin 是本地目录 ../remote.git,不涉及 SSH 登录。
Git 学习路径
返回安装或继续常用命令。需要同时处理多个任务时可阅读 worktree 教程。