初始配置与 SSH
约 447 字大约 1 分钟
布欧-Lewyon
2026-05-15
首页 › Git › 安装与配置(在新窗口打开) › 初始配置与 SSH
用户信息
# 必须配置:提交时记录身份
git config --global user.name "Your Name"
git config --global user.email "your@email.com"配置层级
# system:所有用户(/etc/gitconfig)
git config --system ...
# global:当前用户(~/.gitconfig)
git config --global ...
# local:当前仓库(.git/config,默认级别)
git config --local ...优先级(从低到高):system → global → local。
# 查看所有配置
git config --list
# 查看指定值
git config user.name
git config --global --list
# 编辑配置
git config --global --edit推荐的全局配置
# 默认分支名
git config --global init.defaultBranch main
# 颜色高亮
git config --global color.ui auto
# 默认编辑器
git config --global core.editor vim
# 换行符处理(Windows)
git config --global core.autocrlf true
# macOS / Linux
git config --global core.autocrlf input
# 推送策略
git config --global push.default simple
# 别名(提高效率)
git config --global alias.st status
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.lg "log --oneline --graph --all --decorate"SSH 密钥配置
# 1. 生成密钥
ssh-keygen -t ed25519 -C "your@email.com"
# 或(旧系统不支持 ed25519)
ssh-keygen -t rsa -b 4096 -C "your@email.com"
# 2. 启动 ssh-agent
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519
# 3. 查看公钥
cat ~/.ssh/id_ed25519.pub
# 4. 添加到 GitHub/GitLab
# GitHub: Settings → SSH and GPG keys → New SSH key
# GitLab: Preferences → SSH Keys
# 5. 测试连接
ssh -T git@github.com
# Hi username! You've successfully authenticated...~/.ssh/config
# 管理多个 SSH Key
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_github
Host gitlab.com
HostName gitlab.com
User git
IdentityFile ~/.ssh/id_ed25519_gitlab小结
git config --global user.name/email是必填配置,否则无法提交。- 配置层级:system → global → local,local 覆盖 global。
- SSH 密钥比 HTTPS 密码更方便安全,推荐使用
ed25519算法。 git config --global alias.xxx设置常用命令别名,提升效率。~/.ssh/config管理多个平台的 SSH Key。
上一节:安装 Git 下一节:创建仓库与第一次提交
