用于对 Git 使用的一些笔记,以防自己后面不记得了!
笔记
输入中文
如若需要在git commit提交时输入中文的信息,则需:
git config --global i18n.commitencoding utf-8
其中终端的字符编码、Git编码都需要是同一种字符集编码。
Git配置
全局配置用户名和邮箱
# 设置全局用户名 git config --global user.name "你的用户名" # 设置全局邮箱 git config --global user.email "你的邮箱地址"
让 Git 记住密码
- 使用 credential.helper缓存(推荐)
设置密码缓存,避免每次操作都输入密码:# 缓存密码,默认15分钟 git config --global credential.helper cache # 可以自定义缓存时间(例如设置1小时,单位:秒) git config --global credential.helper 'cache --timeout=3600'
- 永久保存密码(存储在本地)
首次操作时输入一次用户名和密码后,后续就会自动使用保存的凭据。# 永久保存凭据(密码会明文存储在用户目录的.git-credentials文件中) git config --global credential.helper store
- 使用 SSH 密钥(更安全的方式)
- 生成 SSH 密钥:
ssh-keygen -t ed25519 -C "你的邮箱地址"
一路回车使用默认设置即可
- 将公钥添加到 SSH 代理:
# 启动ssh-agent eval "$(ssh-agent -s)" # 添加私钥 ssh-add ~/.ssh/id_ed25519
- 将
~/.ssh/id_ed25519.pub文件中的内容添加到你的 Git 服务(GitHub/GitLab 等)的 SSH 密钥设置中
使用 SSH 地址(如git@github.com:username/repo.git)克隆仓库后,就无需再输入密码了。
- 生成 SSH 密钥:
查看当前配置
# 查看所有Git配置 git config --list # 查看用户名配置 git config user.name # 查看邮箱配置 git config user.email
清除已保存的凭据
如果需要清除已保存的密码,可以使用:
# 清除缓存的凭据 git credential-cache exit # 删除store方式保存的凭据(需要手动编辑文件) rm ~/.git-credentials
推荐使用 SSH 密钥方式,既安全又无需记忆密码;如果使用 HTTPS 地址,则推荐credential.helper cache方式平衡安全性和便利性。
参考
- vscode 命令行使用git commit 无法输入中文
https://juejin.cn/post/7398352956712026150 - git配置记住用户名/密码
https://zhuanlan.zhihu.com/p/1939627380881753863
ChiuYut
2026年05月15日