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 分支操作指南涵盖了日常开发中最常用的场景和命令。

相关推荐
一只大袋鼠9 小时前
Git 进阶(二):分支管理、暂存栈、远程仓库与多人协作
java·开发语言·git
我叫张小白。16 小时前
Git 分支管理与团队协作
git
DogDaoDao18 小时前
Windows 下 Git 报错:`touch` 无法识别 —— 原因分析与 7 种解决方案(从入门到精通)
windows·git·程序员·npm·powershell·cmd·touch
caicai_xiaobai18 小时前
Ubuntu上Git安装步骤
linux·git·ubuntu
come1123419 小时前
git 区分是 Git 分支还是 worktree 路径名
git
憧憬成为java架构高手的小白20 小时前
git多人工作之个人规范使用【ai+个人理解】
git
CVer儿20 小时前
git简单操作
git
Andya_net20 小时前
Git | Git 核心命令深入解析:从原理到实战
大数据·git·elasticsearch
wh_xia_jun21 小时前
给小白的 Maven 命令行执行测试 完整指南
git·maven·intellij-idea
专业白嫖怪21 小时前
H3C UniServer R4950 G5 服务器压测实战:13根内存条24小时压力测试全流程
git