✅ 基础命令 + 高级用法 + ️ 开源实战技巧 + GitHub 社区协作
适合:从0开始 → 熟练开发者 → 参与/维护开源项目
git config --global user.name "你的名字"
git config --global user.email "你的邮箱"
git init
git add .
git commit -m "第一次提交"
git status
git log
git diff # 查看未提交的差异
git diff --cached # 查看已暂存的差异
git remote add origin https://github.com/你/仓库名.git
git push -u origin master # 第一次推送并绑定
git pull # 拉取远程更改
.gitignore
文件忽略不需要上传的文件:
node_modules/
.env
__pycache__/
*.log
git branch new-feature # 创建分支
git checkout new-feature # 切换分支
git merge new-feature # 合并到当前分支
git branch -d new-feature # 删除分支
<<<<<<<
、=======
、>>>>>>>
git add 冲突文件
git commit
Fork
原项目到自己的账户git clone https://github.com/你的用户名/项目名.git
git remote add upstream https://github.com/原作者名/项目名.git
git fetch upstream
git merge upstream/main
典型的开源项目协作流程:
Fork → 新建分支 → 修改代码 → Commit → Push → 提交 Pull Request
详细步骤:
git checkout -b fix-issue-123
git add .
git commit -m "修复了 issue #123"
git push origin fix-issue-123
README.md
包含:
LICENSE
推荐:
CONTRIBUTING.md
建议内容:
在 .github/ISSUE_TEMPLATE/
中添加:
git reset --soft HEAD^ # 回退上一次提交,保留修改
git reset --hard HEAD^ # 强制回退
git revert 提交ID # 创建新的提交来撤销
git stash # 暂存当前更改
git stash list
git stash pop # 恢复更改
git rebase main
适合整理提交,让历史更清晰(配合 interactive rebase
)
.gitignore
忽略 .env
git filter-branch --force --index-filter ...
如:machine-learning
、python
、api
如 GitHub orgs、开源之夏、OpenCollective
示例:

tig
、lazygit
类型 | 资源 |
---|---|
官方文档 | git-scm.com |
可视化教程 | learngitbranching.js.org |
中文教程 | 廖雪峰 Git 教程 |
开源项目指南 | GitHub Docs |