环境配置
本章介绍 GitHub 账号注册、SSH 配置、CLI 工具安装等基础配置。
注册 GitHub 账号
注册步骤
- 访问 github.com
- 点击 "Sign up"
- 输入邮箱、密码和用户名
- 完成邮箱验证
- 选择计划(免费版即可)
用户名建议
- 使用专业、易记的用户名
- 避免使用特殊字符
- 考虑使用真实姓名或常用 ID
个人资料设置
完善个人资料:
- 点击头像 > Settings
- 上传头像
- 填写 Bio(简介)
- 添加个人网站链接
- 设置社交账号
SSH 密钥配置
SSH 密钥用于安全地与 GitHub 通信,避免每次输入密码。
生成 SSH 密钥
ssh-keygen -t ed25519 -C "[email protected]"
如果系统不支持 ed25519,使用 RSA:
ssh-keygen -t rsa -b 4096 -C "[email protected]"
按提示操作,可以:
- 选择保存位置(默认
~/.ssh/id_ed25519) - 设置密码(可选,增加安全性)
添加密钥到 ssh-agent
启动 ssh-agent:
eval "$(ssh-agent -s)"
添加私钥:
ssh-add ~/.ssh/id_ed25519
添加公钥到 GitHub
- 复制公钥内容:
cat ~/.ssh/id_ed25519.pub
- 在 GitHub 上添加:
- 点击头像 > Settings
- 左侧菜单选择 "SSH and GPG keys"
- 点击 "New SSH key"
- 粘贴公钥内容
- 保存
测试连接
ssh -T [email protected]
成功时会显示:
Hi username! You've successfully authenticated, but GitHub does not provide shell access.
配置多个 SSH 密钥
编辑 ~/.ssh/config:
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519
Host github-work
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_work
Personal Access Token
Personal Access Token (PAT) 用于 API 访问和 Git 操作认证。
创建 Token
- 点击头像 > Settings
- 左侧菜单选择 "Developer settings"
- 选择 "Personal access tokens" > "Tokens (classic)"
- 点击 "Generate new token (classic)"
- 选择权限:
repo:仓库访问workflow:Actions 访问write:packages:发布包read:packages:下载包delete:packages:删除包
- 生成并保存 Token
使用 Token
Git 操作时使用 Token 作为密码:
git clone https://github.com/username/repo.git
Username: your_username
Password: your_token
缓存凭证
避免每次输入 Token:
git config --global credential.helper cache
或使用 Git Credential Manager:
- Windows:安装 Git for Windows 时自带
- macOS:
brew install git-credential-manager - Linux:
sudo apt install git-credential-manager
GitHub CLI
GitHub CLI(gh)是 GitHub 官方命令行工具。
安装
Windows:
winget install GitHub.cli
macOS:
brew install gh
Linux:
sudo apt install gh
认证
gh auth login
按提示选择:
- GitHub.com 或 GitHub Enterprise
- 认证方式(浏览器或 Token)
- 协议(HTTPS 或 SSH)
常用命令
仓库操作:
gh repo create my-repo --public
gh repo clone owner/repo
gh repo fork owner/repo
gh repo view owner/repo
Issue 操作:
gh issue create --title "Bug" --body "Description"
gh issue list
gh issue view 123
gh issue close 123
Pull Request 操作:
gh pr create --title "Feature" --body "Description"
gh pr list
gh pr view 456
gh pr checkout 456
gh pr merge 456
Actions 操作:
gh workflow list
gh workflow run workflow-name
gh run list
gh run view
gh run watch
Gist 操作:
gh gist create file.txt
gh gist list
gh gist view abc123
配置别名
gh alias set pc 'pr create'
gh alias set pv 'pr view'
gh alias set ic 'issue create'
Git 配置
基本配置
git config --global user.name "Your Name"
git config --global user.email "[email protected]"
git config --global init.defaultBranch main
查看配置
git config --list
git config user.name
编辑器配置
git config --global core.editor "code --wait"
换行符配置
Windows:
git config --global core.autocrlf true
Linux/macOS:
git config --global core.autocrlf input
.gitignore 配置
创建全局 .gitignore:
git config --global core.excludesfile ~/.gitignore_global
编辑 ~/.gitignore_global:
.DS_Store
.idea/
*.swp
*.log
node_modules/
.env
GPG 签名配置
GPG 签名用于验证提交的真实性。
安装 GPG
Windows:
winget install GnuPG.GnuPG
macOS:
brew install gnupg
Linux:
sudo apt install gnupg
生成 GPG 密钥
gpg --full-generate-key
选择:
- 密钥类型:RSA and RSA
- 密钥长度:4096
- 过期时间:按需选择
- 用户信息:使用 GitHub 邮箱
获取密钥 ID
gpg --list-secret-keys --keyid-format=long
输出示例:
sec rsa4096/3AA5C34371567BD2 2024-01-01 [SC]
密钥 ID 为 3AA5C34371567BD2。
导出公钥
gpg --armor --export 3AA5C34371567BD2
添加到 GitHub
- 复制公钥内容(包括
-----BEGIN和-----END) - GitHub > Settings > SSH and GPG keys
- 点击 "New GPG key"
- 粘贴公钥内容
- 保存
配置 Git 使用 GPG
git config --global user.signingkey 3AA5C34371567BD2
git config --global commit.gpgsign true
双因素认证
强烈建议启用双因素认证(2FA)增强账户安全。
启用 2FA
- Settings > Password and authentication
- 点击 "Enable two-factor authentication"
- 选择认证方式:
- TOTP 应用(推荐)
- SMS
- 保存恢复码
推荐的 TOTP 应用
- Authy
- Google Authenticator
- Microsoft Authenticator
- 1Password