📚 完整教程目录

前言:什么是Git?

🤔 Git的定义

Git是一个分布式版本控制系统,用来跟踪代码的变化。它允许多个开发者协作开发,同时保留完整的历史记录。

为什么需要Git?

Git vs SVN

💡 重点:Git是现代软件开发的必备工具。

安装和配置

安装Git

# Windows # 下载安装程序:https://git-scm.com/download/win # macOS brew install git # Linux sudo apt-get install git # 验证安装 git --version

初始配置

# 配置用户名和邮箱 git config --global user.name "Your Name" git config --global user.email "your@example.com" # 查看配置 git config --list # 配置默认编辑器 git config --global core.editor "vim" # 配置别名 git config --global alias.st status git config --global alias.co checkout git config --global alias.br branch git config --global alias.ci commit

SSH密钥配置

# 生成SSH密钥 ssh-keygen -t rsa -b 4096 -C "your@example.com" # 启动SSH代理 eval "$(ssh-agent -s)" # 添加密钥 ssh-add ~/.ssh/id_rsa # 复制公钥到GitHub cat ~/.ssh/id_rsa.pub

基础命令

初始化仓库

# 创建新仓库 git init # 克隆现有仓库 git clone https://github.com/user/repo.git

提交工作流

# 查看状态 git status # 查看差异 git diff git diff --staged # 添加文件到暂存区 git add file.txt git add . # 添加所有文件 # 提交 git commit -m "Commit message" git commit -am "Commit message" # 跳过add步骤 # 查看历史 git log git log --oneline git log --graph --all --decorate

撤销更改

# 撤销工作区的更改 git checkout file.txt # 撤销暂存区的更改 git reset HEAD file.txt # 撤销最后一次提交(保留更改) git reset --soft HEAD~1 # 撤销最后一次提交(丢弃更改) git reset --hard HEAD~1 # 修改最后一次提交 git commit --amend

分支管理

分支操作

# 查看分支 git branch git branch -a # 包括远程分支 # 创建分支 git branch feature/new-feature # 切换分支 git checkout feature/new-feature git switch feature/new-feature # 新语法 # 创建并切换分支 git checkout -b feature/new-feature git switch -c feature/new-feature # 删除分支 git branch -d feature/new-feature git branch -D feature/new-feature # 强制删除 # 重命名分支 git branch -m old-name new-name

分支策略

Git Flow工作流

# 1. 从develop创建feature分支 git checkout -b feature/login develop # 2. 开发功能 # ... 编写代码 ... git add . git commit -m "Add login feature" # 3. 推送到远程 git push origin feature/login # 4. 创建Pull Request进行代码审查 # 5. 合并到develop git checkout develop git pull origin develop git merge feature/login # 6. 删除feature分支 git branch -d feature/login

合并和冲突解决

合并分支

# 合并分支 git merge feature/new-feature # 创建合并提交 git merge --no-ff feature/new-feature # 变基合并(线性历史) git rebase feature/new-feature

解决冲突

# 冲突标记 <<<<<<< HEAD 当前分支的内容 ======= 要合并的分支的内容 >>>>>>> feature/new-feature # 解决冲突后 git add . git commit -m "Resolve merge conflict" # 中止合并 git merge --abort

高级合并

# 选择性合并(cherry-pick) git cherry-pick commit-hash # 交互式变基 git rebase -i HEAD~3 # 变基时解决冲突 git rebase --continue git rebase --abort

远程仓库

远程操作

# 查看远程仓库 git remote -v # 添加远程仓库 git remote add origin https://github.com/user/repo.git # 修改远程仓库地址 git remote set-url origin https://github.com/user/new-repo.git # 删除远程仓库 git remote remove origin # 推送到远程 git push origin main git push origin feature/new-feature # 拉取远程更新 git pull origin main git fetch origin # 只获取,不合并 # 推送所有分支 git push origin --all # 删除远程分支 git push origin --delete feature/new-feature

跟踪远程分支

# 创建本地分支跟踪远程分支 git checkout -b feature/new-feature origin/feature/new-feature git checkout --track origin/feature/new-feature # 设置上游分支 git branch -u origin/main git branch --set-upstream-to=origin/main

工作流程

完整的开发流程

# 1. 克隆仓库 git clone https://github.com/user/repo.git cd repo # 2. 创建feature分支 git checkout -b feature/user-auth # 3. 开发功能 # ... 编写代码 ... # 4. 提交更改 git add . git commit -m "Add user authentication" # 5. 推送到远程 git push origin feature/user-auth # 6. 创建Pull Request(在GitHub上) # 7. 代码审查和修改 # ... 根据反馈修改代码 ... git add . git commit -m "Fix review comments" git push origin feature/user-auth # 8. 合并到main git checkout main git pull origin main git merge feature/user-auth git push origin main # 9. 删除feature分支 git branch -d feature/user-auth git push origin --delete feature/user-auth

团队协作最佳实践

高级技巧

Stash(暂存更改)

# 暂存当前更改 git stash # 查看暂存列表 git stash list # 恢复暂存 git stash pop git stash apply stash@{0} # 删除暂存 git stash drop stash@{0}

标签(Tag)

# 创建标签 git tag v1.0.0 git tag -a v1.0.0 -m "Version 1.0.0" # 查看标签 git tag git show v1.0.0 # 推送标签 git push origin v1.0.0 git push origin --tags # 删除标签 git tag -d v1.0.0 git push origin --delete v1.0.0

搜索和调试

# 搜索提交 git log --grep="keyword" git log -S "code" # 查找引入bug的提交 git bisect start git bisect bad HEAD git bisect good v1.0.0 # 查看谁修改了某行代码 git blame file.txt # 查看某个文件的历史 git log -p file.txt

GitHub协作

Fork和Pull Request

同步Fork

# 添加上游仓库 git remote add upstream https://github.com/original/repo.git # 拉取上游更新 git fetch upstream # 变基到上游 git rebase upstream/main # 推送到自己的Fork git push origin main

Issue和Discussion

实战项目:团队协作开发

场景:开发一个Web应用

# 1. 项目初始化 git init myapp cd myapp git remote add origin https://github.com/team/myapp.git # 2. 创建初始结构 mkdir src tests docs touch README.md .gitignore git add . git commit -m "Initial commit" git push origin main # 3. 开发者A:开发用户认证功能 git checkout -b feature/auth # ... 编写代码 ... git add . git commit -m "Add user authentication" git push origin feature/auth # 创建Pull Request # 4. 开发者B:开发数据库模块 git checkout -b feature/database # ... 编写代码 ... git add . git commit -m "Add database models" git push origin feature/database # 创建Pull Request # 5. 代码审查和合并 # 在GitHub上审查代码 # 修复反馈的问题 # 合并到main # 6. 同步本地main git checkout main git pull origin main # 7. 删除本地feature分支 git branch -d feature/auth
✨ 实战总结:

通过这个项目,你学会了如何在团队中使用Git进行协作开发。

🎉 Git学习完成

现在你已经掌握了Git版本控制的核心知识。

✅ 你现在可以:

🚀 下一步学习

  1. 学习GitHub Actions自动化
  2. 学习Git Hooks
  3. 学习代码审查最佳实践
  4. 学习持续集成/持续部署(CI/CD)
💡 建议:

Git是团队开发的基础。花时间学好Git,会让你的开发效率大幅提升。