以下是一份详细的GitHub使用实践指南,涵盖从基础到进阶的常用操作,帮助您高效管理代码和协作开发:
注册与SSH配置
# 生成SSH密钥(本地操作)
ssh-keygen -t ed25519 -C "your_email@example.com"
# 将公钥添加到GitHub
cat ~/.ssh/id_ed25519.pub
全局Git配置
git config --global user.name "YourName"
git config --global user.email "your_email@example.com"
git config --global core.editor "code --wait" # 使用VSCode作为默认编辑器
bash
git init
git add .
git commit -m "Initial commit"
git remote add origin git@github.com:username/repo.git
git push -u origin main
bash
git clone git@github.com:username/repo.git
# 或克隆指定分支
git clone -b branch_name git@github.com:username/repo.git
bash
git branch # 查看分支
git checkout -b feature-branch # 创建并切换分支
git push origin feature-branch # 推送新分支到远程
git merge feature-branch # 合并分支(需先切到main分支)
bash
git remote add upstream git@github.com:original_owner/repo.git # 添加源仓库
git fetch upstream # 同步源仓库变更
bash
git pull origin main # 拉取最新代码
# 手动解决冲突后
git add .
git commit -m "Resolve conflicts"
git push
.github/ISSUE_TEMPLATE.md
Fix #123
(自动关闭Issue)yaml
# .github/workflows/ci.yml 示例
name: CI
on: [push]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: npm install && npm test
Git别名提高效率
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.st status
GitHub CLI工具
gh repo clone username/repo # 免SSH配置克隆
gh pr create --web # 通过浏览器创建PR
gh issue list # 查看Issues
大文件存储(Git LFS)
git lfs install
git lfs track "*.psd"
git add .gitattributes
撤销错误提交
git reset --soft HEAD~1 # 保留更改
git reset --hard HEAD~1 # 丢弃更改
恢复误删分支
git checkout -b branch_name SHA # 通过commit哈希恢复
403权限错误
git remote set-url origin git@github.com:user/repo.git
.gitignore
过滤或GitHub Secrets存储通过以上流程,您可以系统性地使用GitHub进行版本控制和团队协作。建议结合图形化工具(如GitHub Desktop/VSCode的Git插件)提升操作直观性。