版本控制
此内容尚不支持你的语言。
版本控制是一种记录文件内容变化,以便将来查阅特定版本修订情况的系统。
为什么需要版本控制?
Section titled “为什么需要版本控制?”- 历史回溯:查看代码的历史变更,回滚到任意版本
- 协作开发:多人同时修改同一项目,自动合并代码
- 分支管理:并行开发多个功能,互不干扰
- 问题定位:通过 Git 日志快速定位引入问题的提交
版本控制系统的分类
Section titled “版本控制系统的分类”本地版本控制系统
Section titled “本地版本控制系统”- 简单的数据库记录文件变更
- 无法多人协作
- 例如:RCS
集中式版本控制系统
Section titled “集中式版本控制系统”- 单一的中央服务器保存所有版本
- 客户端从服务器拉取最新版本
- 优点:易于管理,权限控制
- 缺点:服务器单点故障,依赖网络
- 例如:SVN、CVS、Perforce
分布式版本控制系统
Section titled “分布式版本控制系统”- 每个客户端都有完整的版本库
- 不依赖网络,可以离线工作
- 优点:分布式,性能好,安全
- 例如:Git、Mercurial、Bazaar
Git 简介
Section titled “Git 简介”Git 是由 Linux 之父 Linus Torvalds 为了管理 Linux 内核开发而创建的分布式版本控制系统。
Git 的特点
Section titled “Git 的特点”- 分布式:每个克隆都是完整的版本库
- 快照存储:每次提交保存完整快照,而非差异比较
- 高性能:本地操作,无需联网
- 分支管理:轻量级分支,鼓励频繁使用分支
- 开源免费:完全开源,免费使用
Git 与 SVN 的区别
Section titled “Git 与 SVN 的区别”| 特性 | Git | SVN |
|---|---|---|
| 类型 | 分布式 | 集中式 |
| 存储 | 快照存储 | 差异存储 |
| 分支 | 轻量级指针 | 目录副本 |
| 网络 | 大部分操作无需联网 | 几乎所有操作需要联网 |
| 完整性 | SHA-1 哈希校验 | 较少校验 |
Git 基础
Section titled “Git 基础”安装 Git
Section titled “安装 Git”Windows
Section titled “Windows”- 下载安装包:https://git-scm.com/download/win
- 运行安装程序
- 建议选择 “Git from the command line and also from 3rd-party software”
# 使用 Homebrew 安装brew install git
# 或者使用 Xcode 命令行工具xcode-select --install# Ubuntu/Debiansudo apt-get install git
# CentOS/Fedorasudo yum install git配置 Git
Section titled “配置 Git”# 设置用户名和邮箱(必须)git config --global user.name "Your Name"git config --global user.email "email@example.com"
# 查看配置git config --list
# 查看特定配置git config user.name配置文件位置:
- 系统级:
/etc/gitconfig - 用户级:
~/.gitconfig或~/.config/git/config - 项目级:
.git/config
Git 工作区、暂存区和版本库
Section titled “Git 工作区、暂存区和版本库”工作区 (Working Directory) ↓ git add暂存区 (Staging Area / Index) ↓ git commit版本库 (Repository) ↓ git push远程仓库 (Remote Repository)- 工作区:实际的项目目录,包含正在编辑的文件
- 暂存区:
.git/index文件,保存即将提交的文件列表 - 版本库:
.git目录,包含所有历史版本和元数据
Git 文件状态
Section titled “Git 文件状态”未跟踪 (Untracked) ↓ git add已暂存 (Staged) ↓ git commit已提交 (Committed) / 未修改 (Unmodified) ↓ 修改文件已修改 (Modified) ↓ git add已暂存 (Staged)常用 Git 命令
Section titled “常用 Git 命令”# 初始化仓库git init
# 克隆远程仓库git clone <url>git clone <url> <directory>
# 查看工作区状态git status
# 查看文件变更内容git diff # 工作区 vs 暂存区git diff --staged # 暂存区 vs 版本库git diff HEAD # 工作区 vs 版本库
# 添加文件到暂存区git add <file> # 添加单个文件git add . # 添加所有文件git add -A # 添加所有文件(包括删除的文件)
# 提交到版本库git commit -m "提交信息"git commit -a -m "提交信息" # 自动 add 已跟踪的文件并提交
# 查看提交历史git loggit log --oneline # 简洁模式git log --graph # 图形化展示分支git log -p # 显示具体修改内容git log -5 # 显示最近 5 条# 撤销工作区的修改git checkout -- <file>git restore <file> # Git 2.23+
# 撤销暂存区的修改(回到工作区)git reset HEAD <file>git restore --staged <file> # Git 2.23+
# 撤销最近一次提交(回到暂存区)git reset --soft HEAD~1
# 撤销最近一次提交(回到工作区)git reset --mixed HEAD~1
# 撤销最近一次提交(完全删除,慎用)git reset --hard HEAD~1
# 修改最后一次提交git commit --amendgit commit --amend -m "新的提交信息"# 查看远程仓库git remote -v
# 添加远程仓库git remote add <name> <url>git remote add origin https://github.com/username/repo.git
# 修改远程仓库 URLgit remote set-url origin <new-url>
# 拉取远程仓库git fetch origin # 拉取但不合并git pull origin main # 拉取并合并
# 推送到远程仓库git push origin maingit push -u origin main # 首次推送并关联分支git push --force origin main # 强制推送(慎用)
# 删除远程分支git push origin --delete <branch-name># 查看分支git branch # 查看本地分支git branch -r # 查看远程分支git branch -a # 查看所有分支
# 创建分支git branch <name>
# 切换分支git checkout <name>git switch <name> # Git 2.23+
# 创建并切换分支git checkout -b <name>git switch -c <name> # Git 2.23+
# 合并分支git merge <branch-name> # 合并到当前分支
# 删除分支git branch -d <name> # 已合并的分支git branch -D <name> # 强制删除(未合并)
# 重命名分支git branch -m <old-name> <new-name># 查看标签git tag
# 创建轻量标签git tag v1.0.0
# 创建附注标签git tag -a v1.0.0 -m "版本 1.0.0"
# 查看标签信息git show v1.0.0
# 推送标签git push origin v1.0.0git push origin --tags # 推送所有标签
# 删除本地标签git tag -d v1.0.0
# 删除远程标签git push origin --delete v1.0.0git push origin :v1.0.0# 暂存当前工作区git stashgit stash save "描述信息"
# 查看暂存列表git stash list
# 应用暂存(不删除)git stash applygit stash apply stash@{0}
# 应用并删除暂存git stash popgit stash pop stash@{0}
# 删除暂存git stash dropgit stash drop stash@{0}
# 清空所有暂存git stash clearGit 工作流
Section titled “Git 工作流”Git Flow
Section titled “Git Flow”最经典的分支管理模型:
main ──────────────────────────────────────┐ │ │ │develop ──────────────┼─────────────────────┤ │ │ │feature/xxx ─────────┘ │ │ │release/xxx ────────────────────────────────┤ │ │hotfix/xxx ─────────────────────────────────┘- main:生产环境代码,随时可发布
- develop:开发分支,集成最新功能
- feature:功能分支,从 develop 切出,完成后合并回 develop
- release:发布分支,从 develop 切出,测试后合并到 main 和 develop
- hotfix:紧急修复分支,从 main 切出,修复后合并到 main 和 develop
GitHub Flow
Section titled “GitHub Flow”更简单的工作流,适合持续部署:
- 从 main 创建分支
- 提交代码
- 创建 Pull Request
- 代码审查
- 合并到 main
- 部署
GitLab Flow
Section titled “GitLab Flow”结合 Git Flow 和 GitHub Flow 的优点:
- 使用环境分支(production, staging, pre-production)
- 通过合并到不同分支进行部署
常用 Git 技巧
Section titled “常用 Git 技巧”git config --global alias.st statusgit config --global alias.co checkoutgit config --global alias.br branchgit config --global alias.ci commitgit config --global alias.lg "log --graph --oneline --all"# 搜索提交信息git log --grep="关键字"
# 搜索文件历史git log -p -- <file>
# 搜索代码变更git log -S"关键字"git log -G"正则表达式"# 开始二分查找git bisect start
# 标记当前版本为坏git bisect bad
# 标记已知的好版本git bisect good v1.0.0
# Git 自动切换版本,测试后标记git bisect good/bad
# 找到问题后重置git bisect reset创建 .gitignore 文件:
# 依赖目录node_modules/vendor/__pycache__/
# 构建输出dist/build/*.log
# 系统文件.DS_StoreThumbs.db
# IDE 配置.idea/.vscode/*.swp*.swo
# 环境变量.env.env.local.env.*.localGit 配置示例
Section titled “Git 配置示例”# 用户信息git config --global user.name "Your Name"git config --global user.email "email@example.com"
# 编辑器git config --global core.editor "code --wait" # VS Codegit config --global core.editor "vim" # Vim
# 大小写敏感git config --global core.ignorecase false
# 换行符处理git config --global core.autocrlf input # macOS/Linuxgit config --global core.autocrlf true # Windows
# 显示中文文件名git config --global core.quotepath false
# 拉取时使用 rebasegit config --global pull.rebase true
# 合并时自动创建备份git config --global merge.keepBackup false
# 颜色输出git config --global color.ui autoGit 提交规范
Section titled “Git 提交规范”约定式提交 (Conventional Commits)
Section titled “约定式提交 (Conventional Commits)”<type>(<scope>): <description>
<body>
<footer>Type 类型
Section titled “Type 类型”| 类型 | 描述 |
|---|---|
| feat | 新功能 |
| fix | 修复 bug |
| docs | 文档更新 |
| style | 代码格式(不影响功能) |
| refactor | 重构(不新增功能,不修复 bug) |
| perf | 性能优化 |
| test | 测试相关 |
| chore | 构建/工具相关 |
| ci | CI/CD 相关 |
| revert | 回滚提交 |
feat(auth): 添加登录功能
- 支持用户名密码登录- 支持 OAuth2.0 登录- 添加登录日志
Closes #123fix(user): 修复用户查询的 N+1 问题
使用 JOIN 查询优化性能,查询时间从 500ms 降到 50ms
Closes #456Git 工具推荐
Section titled “Git 工具推荐”GUI 工具
Section titled “GUI 工具”- GitHub Desktop:简单易用,适合初学者
- GitKraken:功能强大,可视化好
- SourceTree:Atlassian 出品,功能丰富
- Tower:macOS 上的优秀 Git 客户端
- VS Code Git:VS Code 内置 Git 支持
- lazygit:简单易用的终端 Git UI
- tig:文本模式的 Git 界面
- gitui:Rust 编写的终端 Git UI
- zsh-git:Oh My Zsh 的 Git 插件
- Learn Git Branching:交互式 Git 学习游戏
- Git-SCM Book:Git 官方书籍
- Pro Git:Git 权威指南(免费电子书)
如何修改已提交的信息?
Section titled “如何修改已提交的信息?”# 修改最后一次提交git commit --amend -m "新的提交信息"
# 如果已经推送到远程,需要强制推送git push --force origin main如何合并多个提交?
Section titled “如何合并多个提交?”# 交互式 rebase,合并最近 3 个提交git rebase -i HEAD~3
# 将 pick 改为 squash 或 s 来合并如何忽略已跟踪的文件?
Section titled “如何忽略已跟踪的文件?”# 停止跟踪但保留文件git rm --cached <file>
# 然后添加到 .gitignore如何解决合并冲突?
Section titled “如何解决合并冲突?”- 运行
git status查看冲突文件 - 打开冲突文件,找到标记为
<<<<<<<,=======,>>>>>>>的部分 - 手动修改,删除标记
git add解决后的文件git commit完成合并
如何找回误删的提交?
Section titled “如何找回误删的提交?”# 查看操作历史git reflog
# 找到误删的 commit hash,恢复git reset --hard <commit-hash># 或者git checkout <commit-hash> -- <file>Git 是现代软件开发中不可或缺的工具。掌握 Git 的使用可以:
- 提高开发效率:快速切换分支、合并代码
- 保证代码安全:完整的历史记录,随时回滚
- 促进团队协作:多人并行开发,自动合并
- 支持持续集成:配合 CI/CD 流程,自动化部署
建议多实践,掌握常用命令后再深入学习高级特性。