git branch 创建分支
git branch 命令用于创建、列出、删除分支。
基本用法
Bash
# 创建新分支
git branch <branch-name>
# 创建新分支并切换
git checkout -b <branch-name>
git switch -c <branch-name>
# 查看所有分支
git branch
# 查看所有分支(含远程)
git branch -a
创建分支示例
Bash
# 创建功能分支
git branch feature/login
# 基于指定提交创建
git branch feature/login abc123
# 基于远程分支创建
git branch feature/login origin/feature
# 创建并切换(推荐)
git checkout -b feature/login
git switch -c feature/login
查看分支
Bash
# 查看本地分支
git branch
# 查看所有分支(含远程)
git branch -a
# 查看远程分支
git branch -r
# 查看分支及最后一次提交
git branch -v
# 查看分支详细信息
git branch -vv
输出示例
Bash
$ git branch
feature/login
* main # * 表示当前分支
develop
$ git branch -v
feature/login a1b2c3d 添加登录页面
* main b2c3d4e 修复首页bug
develop c3d4e5f 合并功能分支
常用选项
| 选项 | 说明 |
|---|---|
| 无参数 | 列出本地分支 |
| -a | 列出所有分支 |
| -r | 列出远程分支 |
| -v | 显示提交信息 |
| -vv | 显示跟踪信息 |
| -d | 删除分支 |
| -D | 强制删除分支 |
重命名分支
Bash
# 重命名分支
git branch -m <old-name> <new-name>
# 重命名当前分支
git branch -m <new-name>
创建分支后不会自动切换,需用 checkout 或 switch 命令切换。
要点总结
git branch <name>创建新分支git branch列出本地分支git branch -a列出所有分支(含远程)git checkout -b或git switch -c创建并切换git branch -m重命名分支
📝 发现内容有误?点击此处直接编辑