Git 分支完整操作指南

Git 分支完整操作指南

1. 查看分支

查看本地分支

bash 复制代码
git branch

查看所有分支(包括远程)

bash 复制代码
git branch -a

查看远程分支

bash 复制代码
git branch -r

查看分支详细信息(最后提交)

bash 复制代码
git branch -v

查看已合并到当前分支的分支

bash 复制代码
git branch --merged

查看未合并到当前分支的分支

bash 复制代码
git branch --no-merged

2. 创建分支

创建新分支并切换

bash 复制代码
git checkout -b 分支名
# 或(推荐,更语义化)
git switch -c 分支名

从特定提交创建分支

bash 复制代码
git branch 分支名 <commit-hash>

从远程分支创建本地分支

bash 复制代码
git checkout -b 本地分支名 origin/远程分支名

3. 切换分支

切换到已有分支

bash 复制代码
git checkout 分支名
# 或
git switch 分支名

切换到上一个分支

bash 复制代码
git checkout -

4. 在新分支中提交代码

创建并切换到新分支

bash 复制代码
git switch -c feature/new-feature

进行代码修改后提交

bash 复制代码
# 查看修改状态
git status

# 添加修改到暂存区
git add .                    # 添加所有修改
git add 文件名              # 添加特定文件

# 提交修改
git commit -m "提交描述"

# 推送到远程仓库(首次推送需要设置上游)
git push -u origin feature/new-feature

后续推送(已设置上游后)

bash 复制代码
git push

5. 合并分支

快速合并(Fast-forward)

bash 复制代码
# 切换到要合并到的目标分支
git checkout main

# 确保目标分支是最新的
git pull origin main

# 合并特性分支
git merge feature/new-feature

非快速合并(创建合并提交)

bash 复制代码
git merge --no-ff feature/new-feature

变基合并(Rebase)

bash 复制代码
# 在特性分支上执行
git checkout feature/new-feature
git rebase main

# 然后切换回主分支合并
git checkout main
git merge feature/new-feature

6. 处理合并冲突

当合并出现冲突时

bash 复制代码
# 合并后查看冲突文件
git status

# 手动解决冲突,然后标记为已解决
git add 冲突解决的文件

# 完成合并提交
git commit

取消合并

bash 复制代码
git merge --abort
git rebase --abort

7. 删除分支

删除本地分支

bash 复制代码
git branch -d 分支名        # 安全删除(已合并)
git branch -D 分支名        # 强制删除(未合并)

删除远程分支

bash 复制代码
git push origin --delete 分支名

8. 分支重命名

重命名当前分支

bash 复制代码
git branch -m 新分支名

重命名指定分支

bash 复制代码
git branch -m 旧分支名 新分支名

9. 实际工作流程示例

标准功能开发流程

bash 复制代码
# 1. 从主分支创建功能分支
git checkout main
git pull origin main
git switch -c feature/user-authentication

# 2. 开发并提交代码
# ... 进行代码修改 ...
git add .
git commit -m "实现用户登录功能"
git push -u origin feature/user-authentication

# 3. 完成开发,合并到主分支
git checkout main
git pull origin main
git merge feature/user-authentication

# 4. 清理分支
git branch -d feature/user-authentication
git push origin --delete feature/user-authentication

紧急修复流程

bash 复制代码
# 从主分支创建热修复分支
git checkout main
git switch -c hotfix/critical-bug

# 修复并提交
git add .
git commit -m "修复严重bug"
git push -u origin hotfix/critical-bug

# 合并到主分支和开发分支
git checkout main
git merge hotfix/critical-bug

git checkout develop
git merge hotfix/critical-bug

# 删除热修复分支
git branch -d hotfix/critical-bug

10. 常用分支命名规范

  • main / master - 主分支
  • develop - 开发分支
  • feature/功能名 - 功能分支
  • release/版本号 - 发布分支
  • hotfix/问题描述 - 热修复分支
  • bugfix/问题描述 - bug修复分支

11. 实用技巧

查看分支图

bash 复制代码
git log --oneline --graph --all

比较分支差异

bash 复制代码
git diff 分支1..分支2

备份当前工作状态

bash 复制代码
git stash        # 保存当前修改
git stash pop    # 恢复保存的修改

这个完整的 Git 分支操作指南涵盖了日常开发中最常用的场景和命令。

相关推荐
十步杀一人_千里不留行5 小时前
Git提交前ESLint校验实践(Husky + lint-staged)
git·github
hh随便起个名8 小时前
适合小白的git的基础使用方法
git
我会一直在的8 小时前
Devps持续集成
git·ci/cd
CoderJia程序员甲9 小时前
GitHub 热榜项目 - 日榜(2026-02-08)
git·ai·开源·llm·github
Serene_Dream11 小时前
git 常用命令
git
jiayong2311 小时前
Detached HEAD 状态详解
git
李少兄20 小时前
在 IntelliJ IDEA 中修改 Git 远程仓库地址
java·git·intellij-idea
先跑起来再说1 天前
Git 入门到实战:一篇搞懂安装、命令、远程仓库与 IDEA 集成
ide·git·后端·elasticsearch·golang·intellij-idea
承渊政道1 天前
Linux系统学习【Linux系统的进度条实现、版本控制器git和调试器gdb介绍】
linux·开发语言·笔记·git·学习·gitee
Doro再努力1 天前
【Linux操作系统12】Git版本控制与GDB调试:从入门到实践
linux·运维·服务器·git·vim