Git / GitHub CLI
安装
macOS
bash
brew install git ghUbuntu
bash
apt update
apt install -y git ghWindows
powershell
winget install --id Git.Git -e --source winget
winget install --id GitHub.cli -e --source winget:::
Windows 中 WinGet 不可用时,可分别从以下地址下载安装:
Git 配置
配置文件路径为
~/.gitconfig
初始配置
bash
# 设置用户名和邮箱
git config --global user.name "your-name"
git config --global user.email "your-email@example.com"
# 查看配置
git config --global --list
# ================ 可选配置 ================
# 设置默认分支
git config --global init.defaultBranch main
# 区分文件名大小写
git config --global core.ignoreCase false
# 忽略文件权限变化
git config --global core.filemode false
# 统一使用 LF 换行
git config --global core.autocrlf false
git config --global core.eol lf
# ================ 代理配置 ================
# 设置代理
git config --global http.proxy http://127.0.0.1:7897
git config --global https.proxy http://127.0.0.1:7897
# 取消代理
git config --global --unset http.proxy
git config --global --unset https.proxySSH
配置 SSH Key
生成 SSH Key:
bash
ssh-keygen -t ed25519 -C "key_name"
# 兼容不支持 Ed25519 的旧环境
# ssh-keygen -t rsa -b 4096 -C "key_name"常用参数:
-t:指定密钥类型。-C:添加注释。-f:指定密钥文件的保存路径和名称。
执行后会在 ~/.ssh 中生成私钥和公钥:
bash
├── id_ed25519
└── id_ed25519.pub将公钥添加到 GitHub 的 Settings → SSH and GPG keys → New SSH key,然后测试连接:
bash
ssh -T git@github.com使用 SSH 地址克隆仓库:
bash
git clone git@github.com:example/repo.git配置多个 SSH Key
生成不同用途的密钥:
bash
ssh-keygen -t ed25519 -C "personal" -f ~/.ssh/id_ed25519_personal
ssh-keygen -t ed25519 -C "work" -f ~/.ssh/id_ed25519_work配置 ~/.ssh/config:
bash
Host github.com
HostName ssh.github.com
Port 443
User git
IdentityFile ~/.ssh/id_ed25519_personal
IdentitiesOnly yes
Host codeup.aliyun.com
HostName codeup.aliyun.com
Port 22
User git
IdentityFile ~/.ssh/id_ed25519_work
IdentitiesOnly yes不同仓库可单独配置用户名和邮箱:
bash
git config user.name "your-name"
git config user.email "your-email@example.com"也可以通过条件配置自动切换账号:
ini
[user]
name = example
email = example@mail.com
[includeIf "gitdir:~/work/**"]
path = ~/.gitconfig-workini
[user]
name = work-name
email = work@example.comGitHub CLI 配置
登录 GitHub:
bash
gh auth login检查登录状态:
bash
gh auth status