EmmmuaCode EmmmuaCode
首页​
导航🚀​
  • 数据结构
  • 计算机网络
  • Java基础

    • JavaSE
    • JVM虚拟机
    • JUC并发编程
  • JavaWeb

    • Servlet
    • MVC
    • filter|listener
  • HTML
  • CSS
  • JavaScript
  • Vue
  • uni-app
  • Spring5
  • SpringMVC
  • SpringBoot2
  • SpringCloud
  • SpringSecurity
  • 搜索引擎

    • ElasticSearch
  • 消息队列

    • RabbitMQ
  • 服务器

    • Nginx🌐
  • 服务框架

    • Dubbo
  • Python基础
  • 数据分析
  • Hadoop
  • SQL 数据库

    • MySQL
  • NoSQL 数据库

    • NoSQL数据库概论
    • Redis
    • MongoDB
    • HBase
  • 框架

    • MyBatis
    • MyBatis-Plus
    • ShardingSphere
  • 部署

    • Linux
    • Docker
  • 管理

    • Maven
    • Git
  • 友情链接
  • 优秀博客文章
  • 索引

    • 分类
    • 标签
    • 归档
  • 其他

    • 关于
Github (opens new window)

wufan

海内存知己,天涯若比邻。
首页​
导航🚀​
  • 数据结构
  • 计算机网络
  • Java基础

    • JavaSE
    • JVM虚拟机
    • JUC并发编程
  • JavaWeb

    • Servlet
    • MVC
    • filter|listener
  • HTML
  • CSS
  • JavaScript
  • Vue
  • uni-app
  • Spring5
  • SpringMVC
  • SpringBoot2
  • SpringCloud
  • SpringSecurity
  • 搜索引擎

    • ElasticSearch
  • 消息队列

    • RabbitMQ
  • 服务器

    • Nginx🌐
  • 服务框架

    • Dubbo
  • Python基础
  • 数据分析
  • Hadoop
  • SQL 数据库

    • MySQL
  • NoSQL 数据库

    • NoSQL数据库概论
    • Redis
    • MongoDB
    • HBase
  • 框架

    • MyBatis
    • MyBatis-Plus
    • ShardingSphere
  • 部署

    • Linux
    • Docker
  • 管理

    • Maven
    • Git
  • 友情链接
  • 优秀博客文章
  • 索引

    • 分类
    • 标签
    • 归档
  • 其他

    • 关于
Github (opens new window)
  • Linux

    • Linux基础篇
    • Linux 目录结构
    • Linux 远程登录到Linux服务器
    • Linux Vi和Vim编辑器
    • Linux 虚拟机关机、重启和用户登录注销
    • Linux 用户管理
    • Linux 实用指令
    • Linux 组管理和权限管理
    • Linux 定时任务调度
    • Linux 磁盘分区、挂载
    • Linux 网络配置
    • Linux 进程管理
    • Linux RPM与YUM
    • Linux 搭建JavaEE环境
    • Linux Shell编程
    • Linux Python开发平台Ubuntu
    • Python定制篇-APT软件管理和远程登录
    • Linux 日志管理
    • Linux 定制自己的Linux
    • Linux 源码介绍&内核升级
    • Linux 备份与恢复
    • Linux 可视化管理-webmin和bt运维工具
  • Docker

    • Docker 简介
    • Docker 安装
    • Docker 常用命令
    • Docker 镜像原理
    • 本地镜像发布到阿里云
    • 将本地镜像推送到私有库
    • Docker 数据卷
    • Docker 软件安装
  • Maven

    • Maven 简介
    • Maven 基础
    • Maven 高级
  • Git 基础

    • Git 概述
    • Git 安装
    • Git 常用命令
    • Git 分支操作
      • 什么是分支
      • 分支的好处
      • 分支的操作
        • 查看分支
        • 创建分支
        • 修改分支
        • 切换分支
        • 合并分支
        • 产生冲突
        • 解决冲突
      • 创建分支和切换分支图解
    • Git 团队协作机制
    • IDEA 集成Git
    • IDEA 集成 GitHub
    • 自建代码托管平台-GitLab
  • studynotes
  • project-management
  • Git
wufan
2022-01-27
目录

Git 分支操作

# Git 分支操作

02

# 什么是分支

在版本控制过程中,同时推进多个任务,为每个任务,我们就可以创建每个任务的单独分支。使用分支意味着程序员可以把自己的工作从开发主线上分离开来,开发自己分支的时候,不会影响主线分支的运行。对于初学者而言,分支可以简单理解为副本,一个分支就是一个单独的副本。(分支底层其实也是指针的引用)

03

# 分支的好处

同时并行推进多个功能开发,提高开发效率。

各个分支在开发过程中,如果某一个分支开发失败,不会对其他分支有任何影响。失败的分支删除重新开始即可。

# 分支的操作

命令名称 作用
git branch 分支名 创建分支
git branch -v 查看分支
git checkout 分支名 切换分支
git merge 分支名 把指定的分支合并到当前分支上

# 查看分支

  1. 基本语法

git branch -v

  1. 案例实操
DELL@FRXcomputer MINGW64 /d/git-Space (master)
$ git branch -v
* master 0765edd my second commit
1
2
3

# 创建分支

  1. 基本语法

git branch 分支名

  1. 案例实操
DELL@FRXcomputer MINGW64 /d/git-Space (master)
$ git branch hot-fix

DELL@FRXcomputer MINGW64 /d/git-Space (master)
$ git branch -v
  hot-fix 0765edd my second (刚创建的新的分支,并将主分支 master的内容复制了一份)
* master  0765edd my second commit
1
2
3
4
5
6
7

# 修改分支

  • 在master分支上做修改
DELL@FRXcomputer MINGW64 /d/git-Space (master)
$ vim hello.txt
1
2
  • 添加暂存区
DELL@FRXcomputer MINGW64 /d/git-Space (master)
$ git add hello.txt
1
2
  • 提交本地库
DELL@FRXcomputer MINGW64 /d/git-Space (master)
$ git commit -m"my third commit" hello.txt
[master 019a8dd] my third commit
 1 file changed, 10 insertions(+)
1
2
3
4
  • 查看分支
DELL@FRXcomputer MINGW64 /d/git-Space (master)
$ git branch -v
  hot-fix 0765edd my second commit(hot-fix 分支并未做任何改变)
* master  019a8dd my third commit(当前 master 分支已更新为最新一次提交的版本)
1
2
3
4
  • 查看master分支上的文件内容
DELL@FRXcomputer MINGW64 /d/git-Space (master)
$ cat hello.txt
hello git! hi
hello git! hi
hello git! hi
hello git! hi
hello git! hi
hello git! hi
hello git! hi
hello git! hi
hello git! hi
hello git! hi
hello git! hi master分支
1
2
3
4
5
6
7
8
9
10
11
12
13

# 切换分支

  1. 基本语法

git checkout 分支名

  1. 案例实操
DELL@FRXcomputer MINGW64 /d/git-Space (master)
$ git checkout hot-fix
Switched to branch 'hot-fix'
DELL@FRXcomputer MINGW64 /d/git-Space (hot-fix)
1
2
3
4
  • 查看 hot-fix 分支上的文件内容发现与 master 分支上的内容不同
DELL@FRXcomputer MINGW64 /d/git-Space (hot-fix)
$ cat hello.txt
hello git! hi
1
2
3
  • 在 hot-fix 分支上做修改
DELL@FRXcomputer MINGW64 /d/git-Space (hot-fix)
$ vim hello.txt

DELL@FRXcomputer MINGW64 /d/git-Space (hot-fix)
$ cat hello.txt
hello git! hi
hello git! hi
hello git! hi
hello git! hi
hello git! hi
hello git! hi
hello git! hi
hello git! hi
hello git! hi
hello git! hi
hello git! hi hot-fix 分支
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  • 添加到暂存区
DELL@FRXcomputer MINGW64 /d/git-Space (hot-fix)
$ git add hello.txt
1
2
  • 提交到本地库
DELL@FRXcomputer MINGW64 /d/git-Space (hot-fix)
$ git commit -m"hot-fix commit" hello.txt
[hot-fix 4031a66] hot-fix commit
 1 file changed, 10 insertions(+)
1
2
3
4

# 合并分支

  1. 基本语法

git merge 分支名

  1. 案例实操
DELL@FRXcomputer MINGW64 /d/git-Space (master)
$ git merge hot-fix
Auto-merging hello.txt
CONFLICT (content): Merge conflict in hello.txt
Automatic merge failed; fix conflicts and then commit the result.
1
2
3
4
5

# 产生冲突

冲突产生的表现:后面状态为 MERGING

DELL@FRXcomputer MINGW64 /d/git-Space (master|MERGING)
$ cat hello.txt
hello git! hi
hello git! hi
hello git! hi
hello git! hi
hello git! hi
hello git! hi
hello git! hi
hello git! hi
hello git! hi
hello git! hi
<<<<<<< HEAD
hello git! hi master分支
=======
hello git! hi hot-fix 分支
>>>>>>> hot-fix
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
  • 冲突产生的原因
    • 合并分支时,两个分支在同一个文件的同一个位置有两套完全不同的修改。Git 无法替我们决定使用哪一个。必须人为决定新代码内容。

查看状态(检测到有文件有两处修改)

DELL@FRXcomputer MINGW64 /d/git-Space (master|MERGING)
$ git status
On branch master
You have unmerged paths.
  (fix conflicts and run "git commit")
  (use "git merge --abort" to abort the merge)

Unmerged paths:
  (use "git add <file>..." to mark resolution)
        both modified:   hello.txt

no changes added to commit (use "git add" and/or "git commit -a")
1
2
3
4
5
6
7
8
9
10
11
12

# 解决冲突

  1. 编辑有冲突的文件,删除特殊符号,决定要使用的内容

特殊符号:<<<<<<< HEAD 当前分支的代码 ======= 合并过来的代码 >>>>>>> hot-fix

DELL@FRXcomputer MINGW64 /d/git-Space (master|MERGING)
$ vim hello.txt

DELL@FRXcomputer MINGW64 /d/git-Space (master|MERGING)
$ cat hello.txt
hello git! hi
hello git! hi
hello git! hi
hello git! hi
hello git! hi
hello git! hi
hello git! hi
hello git! hi
hello git! hi
hello git! hi master分支
hello git! hi hot-fix分支
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

提示

合并分支会修改当前的分支(例如master),不会修改hot-fix分支

  1. 添加到暂存区
DELL@FRXcomputer MINGW64 /d/git-Space (master|MERGING)
$ git add hello.txt
1
2
  1. 执行提交(注意:此时使用 git commit 命令时不能带文件名)
DELL@FRXcomputer MINGW64 /d/git-Space (master|MERGING)
$ git commit -m"merge hot-fix"
[master 742c681] merge hot-fix

DELL@FRXcomputer MINGW64 /d/git-Space (master)
1
2
3
4
5

发现后面 MERGING 消失,变为正常

# 创建分支和切换分支图解

01

master、hot-fix 其实都是指向具体版本记录的指针。当前所在的分支,其实是由 HEAD决定的。所以创建分支的本质就是多创建一个指针。

HEAD 如果指向 master,那么我们现在就在 master 分支上。

HEAD 如果执行 hotfix,那么我们现在就在 hotfix 分支上。

所以切换分支的本质就是移动 HEAD 指针。

#Git
上次更新: 2024/04/21, 09:42:22
Git 常用命令
Git 团队协作机制

← Git 常用命令 Git 团队协作机制→

最近更新
01
微信支付功能的实现与流程
11-21
02
购物车与结算区域的深入优化与功能完善
11-21
03
购物车与结算区域的功能实现与优化
11-21
更多文章>
Theme by Vdoing | Copyright © 2023-2024 EmmmuaCode | 黔ICP备2022009864号-2
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式