环境配置
本章介绍 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 官方命令行工具,让你可以在终端中高效地操作 GitHub,无需频繁切换到浏览器。
安装
Windows:
# 使用 winget
winget install GitHub.cli
# 使用 Chocolatey
choco install gh
# 使用 Scoop
scoop install gh
或者从 GitHub CLI 发布页面 下载 MSI 安装包。
macOS:
# 使用 Homebrew(推荐)
brew install gh
# 使用 MacPorts
sudo port install gh
Linux:
# Debian/Ubuntu/Raspberry Pi
curl -fsSL https://cli.github.com/packages/githubcli-archive-keyring.gpg | sudo dd of=/usr/share/keyrings/githubcli-archive-keyring.gpg
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" | sudo tee /etc/apt/sources.list.d/github-cli.list > /dev/null
sudo apt update
sudo apt install gh
# Fedora/CentOS/RHEL
sudo dnf install gh
# Arch Linux
sudo pacman -S github-cli
Codespaces 和 Actions:
GitHub Codespaces 和 GitHub Actions 运行器已经预装了 GitHub CLI,无需额外安装。如果需要在 Dev Container 中安装,可以添加:
{
"features": {
"ghcr.io/devcontainers/features/github-cli:1": {}
}
}
认证
安装后,首先需要进行认证:
gh auth login
认证流程会引导你完成以下选择:
- GitHub 实例:选择
GitHub.com或GitHub Enterprise - 协议:选择
HTTPS或SSH- HTTPS:更简单,适合大多数用户
- SSH:需要配置 SSH 密钥,但更安全
- 认证方式:
- 浏览器:打开浏览器完成 OAuth 认证(推荐)
- Token:使用 Personal Access Token 认证
如果选择浏览器认证,GitHub CLI 会生成一个一次性代码,你需要在浏览器中输入该代码完成授权。
多账户管理
如果你有多个 GitHub 账户(如个人账户和工作账户),可以同时认证并切换:
# 登录第一个账户
gh auth login
# 登录第二个账户
gh auth login -h github.com
# 查看所有已认证账户
gh auth status
# 切换活跃账户
gh auth switch -u username
gh auth switch -h github.enterprise.com
使用 Token 认证
如果你已经有 Personal Access Token,可以直接使用:
# 从标准输入读取 Token
echo YOUR_TOKEN | gh auth login --with-token
# 或从文件读取
cat token.txt | gh auth login --with-token
验证认证状态
gh auth status
输出示例:
github.com
✓ Logged in to github.com as username (/Users/you/.config/gh/hosts.yml)
✓ Git operations for github.com configured to use ssh protocol.
✓ Token: gho_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
✓ Token scopes: gist, read:org, repo, workflow
常用命令
GitHub CLI 提供了丰富的命令来操作 GitHub 的各种功能。以下是最常用的命令示例。
仓库操作:
# 创建仓库
gh repo create my-repo --public # 创建公开仓库
gh repo create my-repo --private # 创建私有仓库
gh repo create --source=. --remote=origin # 从当前目录创建并添加远程
# 克隆和 Fork
gh repo clone owner/repo # 克隆仓库
gh repo fork owner/repo # Fork 仓库
gh repo fork owner/repo --clone # Fork 并克隆到本地
# 查看
gh repo view owner/repo # 查看仓库信息
gh repo view --web # 在浏览器中打开
# 列出仓库
gh repo list # 列出你的仓库
gh repo list owner --limit 50 # 列出指定用户的仓库
Issue 操作:
# 创建 Issue
gh issue create # 交互式创建
gh issue create -t "Bug 报告" -b "描述内容" # 快速创建
# 列出和查看
gh issue list # 列出所有开放的 Issue
gh issue list -l bug # 按标签筛选
gh issue list -a @me # 列出分配给我的 Issue
gh issue view 123 # 查看详情
# 状态管理
gh issue close 123 # 关闭
gh issue reopen 123 # 重新打开
Pull Request 操作:
# 创建 PR
gh pr create # 交互式创建
gh pr create -t "新功能" -b "描述" # 快速创建
gh pr create --draft # 创建草稿 PR
# 列出和查看
gh pr list # 列出所有开放的 PR
gh pr list -a @me # 列出我创建的 PR
gh pr list --review-requested @me # 列出等待我审查的 PR
gh pr view 456 # 查看详情
# 检出和合并
gh pr checkout 456 # 检出 PR 到本地分支
gh pr merge 456 # 合并 PR
gh pr merge 456 --squash # Squash 合并
# 审查
gh pr review 456 --approve # 批准
gh pr review 456 --request-changes # 请求更改
Actions 操作:
# 工作流管理
gh workflow list # 列出所有工作流
gh workflow run workflow-name # 触发工作流
gh workflow run workflow-name -f env=staging # 带参数触发
# 运行管理
gh run list # 列出运行记录
gh run view # 查看最近的运行
gh run watch # 实时查看运行进度
gh run rerun # 重新运行
Gist 操作:
gh gist create file.txt # 创建私有 Gist
gh gist create file.txt --public # 创建公开 Gist
gh gist list # 列出你的 Gist
gh gist view abc123 # 查看 Gist
gh gist clone abc123 # 克隆 Gist 到本地
搜索功能:
gh search repos "react" --language javascript # 搜索仓库
gh search issues "bug" --state open # 搜索 Issue
gh search prs "feature" --author @me # 搜索 PR
gh search code "function" --repo owner/repo # 搜索代码
配置别名
为常用命令创建简短的别名可以大大提高效率:
# PR 相关
gh alias set pc 'pr create' # gh pc = gh pr create
gh alias set pv 'pr view' # gh pv = gh pr view
gh alias set pl 'pr list' # gh pl = gh pr list
gh alias set pm 'pr merge' # gh pm = gh pr merge
# Issue 相关
gh alias set ic 'issue create' # gh ic = gh issue create
gh alias set il 'issue list' # gh il = gh issue list
gh alias set iv 'issue view' # gh iv = gh issue view
# 组合命令
gh alias set myissues 'issue list -a @me' # 列出分配给我的 Issue
gh alias set myprs 'pr list -a @me' # 列出我创建的 PR
gh alias set review 'pr list --review-requested @me' # 等待我审查的 PR
# 管理别名
gh alias list # 列出所有别名
gh alias delete pc # 删除别名
别名设置后,可以直接使用短命令:
gh pc -t "新功能" -b "描述" # 等同于 gh pr create -t "新功能" -b "描述"
gh myissues # 等同于 gh issue list -a @me
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