git branch 命令
简介
git branch 是 Git 中用于管理分支的命令。分支是 Git 的核心功能之一,允许你在不同的开发线上并行工作,而不会相互干扰。
基本概念
什么是分支
分支是 Git 中指向提交的指针。每个分支代表一条独立的开发线,可以在不影响其他分支的情况下进行开发。
分支的作用
- 并行开发:多个功能可以同时开发
- 实验性更改:在不影响主分支的情况下尝试新功能
- 版本管理:维护不同的版本(如稳定版、开发版)
- 功能隔离:每个功能在独立分支上开发
命令语法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| git branch [选项]
git branch <分支名> [起始点]
git branch -d <分支名> git branch -D <分支名>
git branch -m <旧名称> <新名称> git branch -m <新名称>
git branch -v git branch -vv
|
查看帮助文档
常用选项
| 选项 |
说明 |
-a, --all |
显示所有分支(本地和远程) |
-r, --remotes |
只显示远程分支 |
-v, --verbose |
显示详细信息(最后提交) |
-vv |
显示详细信息(包括跟踪分支) |
-d, --delete |
删除分支(需要分支已合并) |
-D |
强制删除分支(即使未合并) |
-m, --move |
重命名分支 |
-M |
强制重命名分支 |
-c, --copy |
复制分支 |
-C |
强制复制分支 |
--set-upstream |
设置上游分支 |
--unset-upstream |
取消上游分支设置 |
--track |
创建跟踪分支 |
--no-track |
不创建跟踪分支 |
--merged |
只显示已合并的分支 |
--no-merged |
只显示未合并的分支 |
-l, --list |
列出分支(默认行为) |
--contains <commit> |
只显示包含指定提交的分支 |
--no-contains <commit> |
只显示不包含指定提交的分支 |
基本使用
1. 列出分支
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| $ git branch
$ git branch -a
$ git branch -r
$ git branch -v
$ git branch -vv
|
示例输出:
1 2 3 4 5
| * main develop feature/login remotes/origin/main remotes/origin/develop
|
* 表示当前所在的分支。
2. 创建分支
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| $ git branch feature/new-feature
$ git branch feature/new-feature main
$ git branch hotfix abc1234
$ git branch release/v1.0.0 v1.0.0
$ git checkout -b feature/new-feature $ git switch -c feature/new-feature
|
3. 删除分支
1 2 3 4 5 6 7 8
| $ git branch -d feature/old-feature
$ git branch -D feature/old-feature
$ git push origin --delete feature/old-feature
|
注意:
- 不能删除当前所在的分支
-d 只能删除已合并的分支
-D 可以强制删除,但会丢失未合并的更改
4. 重命名分支
1 2 3 4 5 6 7 8 9
| $ git branch -m new-branch-name
$ git checkout main $ git branch -m old-name new-name
$ git branch -M new-name
|
5. 查看分支信息
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| $ git branch -v
$ git branch -vv
$ git branch --merged
$ git branch --no-merged
$ git branch --contains abc1234
|
6. 设置跟踪分支
1 2 3 4 5 6 7 8
| $ git branch --set-upstream-to=origin/main main
$ git branch --track local-branch origin/remote-branch
$ git branch --unset-upstream
|
高级用法
1. 复制分支
1 2 3 4 5
| $ git branch -c source-branch new-branch
$ git branch -C source-branch new-branch
|
2. 基于特定提交创建分支
1 2 3 4 5
| $ git branch feature/fix abc1234
$ git branch hotfix HEAD~3
|
3. 列出特定条件下的分支
1 2 3 4 5 6 7 8 9 10 11
| $ git branch --merged main
$ git branch --no-merged main
$ git branch --contains feature-commit
$ git branch --no-contains bug-commit
|
4. 批量操作
1 2 3 4 5 6
| $ git branch --merged | grep -v "\*\|main" | xargs git branch -d
$ git remote prune origin $ git branch -vv | grep ': gone]' | awk '{print $1}' | xargs git branch -d
|
实际应用场景
场景1:创建功能分支
1 2 3 4 5 6 7 8
| $ git checkout main $ git pull origin main $ git branch feature/user-profile $ git checkout feature/user-profile
$ git checkout -b feature/user-profile main
|
场景2:清理已合并的分支
1 2 3 4 5 6 7 8 9
| $ git branch --merged
$ git branch -d feature/old-feature-1 $ git branch -d feature/old-feature-2
$ git branch --merged | grep feature/ | xargs git branch -d
|
场景3:重命名分支
1 2 3 4 5 6
| $ git branch -m feature/new-name
$ git checkout main $ git branch -m feature/old-name feature/new-name
|
场景4:查看分支关系
1 2 3 4 5
| $ git branch -vv
$ git log --oneline --graph --all --decorate
|
场景5:创建发布分支
1 2 3 4 5
| $ git branch release/v1.0.0 v1.0.0
$ git checkout -b release/v1.0.0 main
|
场景6:设置跟踪分支
1 2 3 4 5
| $ git branch --track local-feature origin/feature
$ git branch --set-upstream-to=origin/main main
|
与其他命令的组合
git branch + git checkout
1 2 3 4 5
| $ git checkout -b new-branch
$ git switch -c new-branch
|
git branch + git merge
1 2 3 4
| $ git checkout main $ git merge feature-branch $ git branch -d feature-branch
|
git branch + git push
1 2 3 4 5 6
| $ git branch feature/new $ git push -u origin feature/new
$ git push origin --delete feature/old
|
常见问题和解决方案
问题1:无法删除当前分支
错误信息:
1
| error: Cannot delete branch 'branch-name' checked out at '/path'
|
解决方案:
1 2 3
| $ git checkout main $ git branch -d branch-name
|
问题2:分支未合并无法删除
错误信息:
1
| error: The branch 'branch-name' is not fully merged
|
解决方案:
1 2 3 4 5 6
| $ git merge branch-name $ git branch -d branch-name
$ git branch -D branch-name
|
问题3:分支名冲突
问题:本地和远程有同名分支但内容不同
解决方案:
1 2 3 4 5
| $ git branch -m local-branch-name
$ git branch feature/local-feature
|
问题4:查看远程分支
问题:看不到远程分支
解决方案:
1 2 3 4 5 6 7 8
| $ git fetch origin
$ git branch -r
$ git branch -a
|
问题5:跟踪分支未设置
问题:推送时提示未设置上游分支
解决方案:
1 2 3 4 5
| $ git branch --set-upstream-to=origin/branch-name branch-name
$ git push -u origin branch-name
|
最佳实践
1. 使用描述性的分支名
1 2 3 4 5 6 7 8 9
| $ git branch feature/user-authentication $ git branch bugfix/login-error $ git branch hotfix/security-patch
$ git branch test $ git branch fix $ git branch new
|
2. 定期清理已合并的分支
1 2
| $ git branch --merged | grep -v "\*\|main\|develop" | xargs git branch -d
|
3. 使用分支保护
4. 保持分支同步
1 2 3 4 5 6 7
| $ git checkout main $ git pull origin main
$ git checkout feature-branch $ git rebase main
|
5. 使用分支命名规范
常见的命名规范:
feature/功能名:新功能
bugfix/问题描述:bug 修复
hotfix/紧急修复:紧急修复
release/版本号:发布分支
develop:开发分支
main/master:主分支
6. 删除远程分支
1 2 3 4 5
| $ git push origin --delete branch-name
$ git remote prune origin
|
总结
git branch 是 Git 分支管理的核心命令,掌握它可以帮助你:
- 创建分支:为不同功能创建独立分支
- 查看分支:了解所有分支的状态
- 删除分支:清理不需要的分支
- 管理分支:重命名、复制、设置跟踪
关键要点:
- ✅ 使用描述性的分支名
- ✅ 定期清理已合并的分支
- ✅ 保持分支同步
- ✅ 遵循分支命名规范
- ❌ 不要删除未合并的重要分支
- ❌ 不要强制删除可能包含重要更改的分支
通过合理使用 git branch,可以更好地组织和管理 Git 仓库的分支结构。