Appearance
Git 分支管理
分支是Git的核心功能之一,它允许你在同一项目中同时进行多个独立的工作线程。本章详细介绍Git分支的创建、切换、合并和管理。
分支基础概念
什么是分支
Git分支本质上是指向提交对象的可移动指针。默认分支通常名为main或master。
bash
# 查看所有分支
git branch
# 查看分支的详细信息
git branch -v
# 查看远程分支
git branch -r
# 查看所有分支(本地和远程)
git branch -a
分支工作原理
Git的分支非常轻量,创建和切换分支几乎是瞬间完成的,因为它们只是指向提交对象的指针。
创建分支
创建新分支
bash
# 创建新分支但不切换
git branch feature-branch
# 创建并切换到新分支
git checkout -b feature-branch
# Git 2.23+的推荐方式
git switch -c feature-branch
# 基于特定提交创建分支
git checkout -b feature-branch commit-hash
# 基于远程分支创建本地分支
git checkout -b feature-branch origin/feature-branch
git switch -c feature-branch origin/feature-branch
从远程创建分支
bash
# 获取远程分支列表
git fetch origin
# 创建并切换到远程分支的本地副本
git checkout -b feature origin/feature
git switch -c feature origin/feature
# 或者使用track选项
git checkout --track origin/feature-branch
切换分支
基本切换操作
bash
# 切换到已存在的分支
git checkout branch-name
git switch branch-name
# 切换到上一个分支
git checkout -
git switch -
# 临时切换(使用工作树)
git worktree add ../project-feature feature-branch
分支切换注意事项
bash
# 如果工作区有未提交的更改,可能会阻止切换
# 解决方法1:暂存更改
git stash
git checkout other-branch
git stash pop
# 解决方法2:提交更改
git add .
git commit -m "WIP: save changes"
git checkout other-branch
# 解决方法3:丢弃更改(谨慎使用)
git checkout other-branch -f
分支操作
重命名分支
bash
# 重命名当前分支
git branch -m new-name
# 重命名指定分支
git branch -m old-name new-name
# 强制重命名(如果新名称已存在)
git branch -M new-name
删除分支
bash
# 删除已合并的本地分支
git branch -d branch-name
# 强制删除分支(不检查是否已合并)
git branch -D branch-name
# 删除远程分支
git push origin --delete branch-name
git push origin :branch-name # 旧语法
# 清理远程分支的本地引用
git remote prune origin
git fetch --prune
分支合并
基本合并操作
bash
# 切换到目标分支并合并
git checkout main
git merge feature-branch
# 合并时创建合并提交
git merge --no-ff feature-branch
# 合并时使用快进模式
git merge --ff-only feature-branch
# 合并时禁用快进模式
git merge --no-ff feature-branch
合并策略
bash
# 三方合并(默认)
git merge feature-branch
# 变基合并
git rebase main
git checkout main
git merge feature-branch
# 樱桃拣选(选择性合并特定提交)
git cherry-pick commit-hash
# 合并特定提交
git merge commit-hash
分支查看和比较
查看分支关系
bash
# 查看分支图
git log --graph --oneline --all
# 查看分支包含的提交
git log branch-name ^main
# 查看两个分支之间的差异
git log main..feature-branch
# 查看哪些提交在哪个分支上
git branch --contains commit-hash
# 查看分支的合并情况
git branch --merged
git branch --no-merged
比较分支
bash
# 比较两个分支的文件差异
git diff main..feature-branch
# 比较分支的提交历史
git log main..feature-branch
# 统计差异
git diff --stat main..feature-branch
高级分支操作
分支重置和变基
bash
# 交互式变基(修改提交历史)
git rebase -i HEAD~3
# 变基到特定分支
git rebase main
# 中断变基过程
git rebase --abort
# 继续变基过程
git rebase --continue
# 跳过当前提交
git rebase --skip
分支追踪
bash
# 设置上游分支
git branch --set-upstream-to=origin/branch-name
# 查看追踪关系
git branch -vv
# 推送并设置上游分支
git push -u origin feature-branch
# 删除追踪关系
git branch --unset-upstream
分支工作流
Git Flow工作流
bash
# Git Flow的基本分支结构
# main: 生产分支
# develop: 开发分支
# feature/*: 功能分支
# release/*: 发布分支
# hotfix/*: 热修复分支
# 开始新功能
git checkout develop
git pull origin develop
git checkout -b feature/new-feature
# 完成功能后合并回develop
git checkout develop
git merge --no-ff feature/new-feature
git branch -d feature/new-feature
# 开始发布
git checkout -b release/v1.0.0 develop
# 完成发布
git checkout main
git merge --no-ff release/v1.0.0
git tag -a v1.0.0 -m "Release version 1.0.0"
git checkout develop
git merge --no-ff release/v1.0.0
git branch -d release/v1.0.0
GitHub Flow工作流
bash
# GitHub Flow的基本流程
# 1. 从main创建功能分支
git checkout main
git pull origin main
git checkout -b feature-branch
# 2. 开发并提交更改
git add .
git commit -m "Add new feature"
# 3. 推送到远程
git push origin feature-branch
# 4. 创建Pull Request
# 5. 代码审查和合并
# 6. 删除功能分支
git branch -d feature-branch
git push origin --delete feature-branch
GitLab Flow工作流
bash
# GitLab Flow结合了Git Flow和GitHub Flow的优点
# 使用环境分支(如pre-production)
# 开发流程
git checkout main
git pull origin main
git checkout -b feature-branch
# 推送功能分支
git push origin feature-branch
# 合并到main
git checkout main
git merge --no-ff feature-branch
# 部署到预发布环境
git checkout pre-production
git merge --no-ff main
# 部署到生产环境
git checkout production
git merge --no-ff main
分支管理最佳实践
分支命名规范
bash
# 推荐的分支命名规范
feature/user-authentication # 功能分支
bugfix/login-error # 修复分支
hotfix/critical-security # 紧急修复
release/v2.1.0 # 发布分支
docs/update-readme # 文档分支
分支清理
bash
# 删除已合并的分支
git branch --merged | grep -v "\*\|main\|master" | xargs -n 1 git branch -d
# 使用Git的高级功能清理
git remote prune origin
git branch -vv | grep 'origin/.*: gone]' | awk '{print $1}' | xargs git branch -D
分支配置
bash
# 配置分支自动设置上游
git config --global push.default simple
# 配置合并策略
git config --global merge.tool vimdiff
# 配置变基时自动设置上游
git config --global branch.autoSetupRebase always
实用技巧
分支操作快捷方式
bash
# 创建并切换到新分支,同时推送并设置上游
git checkout -b feature-branch
git push -u origin feature-branch
# 或者使用别名
git config --global alias.push-new '!git push -u origin $(git branch --show-current)'
# 一键删除远程分支
git config --global alias.delete-remote '!git push origin --delete $(git branch --show-current)'
# 查看分支的提交统计
git config --global alias.stats 'log --oneline --graph --all'
分支可视化
bash
# 使用git log查看分支图
git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit
# 使用gitk图形界面
gitk --all
# 使用git gui
git gui
通过掌握这些分支管理技能,你可以更有效地组织和管理项目开发流程,实现多人协作和功能隔离。