04.git分支操作

04.git分支操作

image-20210904191135798

一.什么是分支?

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

二.分支的好处

同时并行推进多个功能开发,提高开发效率。
各个分支在开发过程中,如果某一个分支开发失败,不会对其他分支有任何影响。失败的分支删除重新开始即可。

三.分支的操作

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

3.1 查看分支

git branch -v 查看分支

Jason@JASON MINGW64 ~/Desktop/GitTest (master)
$ git branch -v(查看分支)
* master 761404b third commit

Jason@JASON MINGW64 ~/Desktop/GitTest (master)
$ git branch hot-fix(创建分支)

Jason@JASON MINGW64 ~/Desktop/GitTest (master)
$ git branch -v (查看分支)
  hot-fix 761404b third commit
* master  761404b third commit

3.2 创建分支

git branch 分支名

Jason@JASON MINGW64 ~/Desktop/GitTest (master)
$ git branch hot-fix(创建分支)

Jason@JASON MINGW64 ~/Desktop/GitTest (master)
$ git branch -v (查看分支)
  hot-fix 761404b third commit
* master  761404b third commit
image-20210904193612631

3.3 切换分支

git checkout 分支名

Jason@JASON MINGW64 ~/Desktop/GitTest (master)
$ git checkout hot-fix(切换hot-fix分支)
Switched to branch 'hot-fix'

Jason@JASON MINGW64 ~/Desktop/GitTest (hot-fix)
$ git branch -v(查看分支)
* hot-fix 761404b third commit
  master  761404b third commit

3.2.1 修改分支

Jason@JASON MINGW64 ~/Desktop/GitTest (master)
$ git checkout hot-fix
Switched to branch 'hot-fix'

Jason@JASON MINGW64 ~/Desktop/GitTest (hot-fix)
$ git branch -v
* hot-fix 761404b third commit
  master  761404b third commit

Jason@JASON MINGW64 ~/Desktop/GitTest (hot-fix)
$ ^C

Jason@JASON MINGW64 ~/Desktop/GitTest (hot-fix)
$ cat hello.txt
hello world!hello Git!1111
hello world!hello Git!2222
hello world!hello Git!
hello world!hello Git!
hello world!hello Git!
hello world!hello Git!
hello world!hello Git!


Jason@JASON MINGW64 ~/Desktop/GitTest (hot-fix)
$ vim hello.txt

Jason@JASON MINGW64 ~/Desktop/GitTest (hot-fix)
$ cat hello.txt
hello world!hello Git!hot-fix分支
hello world!hello Git!
hello world!hello Git!
hello world!hello Git!
hello world!hello Git!
hello world!hello Git!
hello world!hello Git!


Jason@JASON MINGW64 ~/Desktop/GitTest (hot-fix)
$ git status
On branch hot-fix
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   hello.txt

no changes added to commit (use "git add" and/or "git commit -a")

Jason@JASON MINGW64 ~/Desktop/GitTest (hot-fix)

image-20210904194644340

3.2.2 添加暂存区,提交到本地库

Jason@JASON MINGW64 ~/Desktop/GitTest (hot-fix)
$ cat hello.txt
hello world!hello Git!hot-fix分支
hello world!hello Git!
hello world!hello Git!
hello world!hello Git!
hello world!hello Git!
hello world!hello Git!
hello world!hello Git!


Jason@JASON MINGW64 ~/Desktop/GitTest (hot-fix)
$ git status
On branch hot-fix
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   hello.txt

no changes added to commit (use "git add" and/or "git commit -a")

Jason@JASON MINGW64 ~/Desktop/GitTest (hot-fix)
$ ^C

Jason@JASON MINGW64 ~/Desktop/GitTest (hot-fix)
$ git add hello.txt

Jason@JASON MINGW64 ~/Desktop/GitTest (hot-fix)
$ git status
On branch hot-fix
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        modified:   hello.txt


Jason@JASON MINGW64 ~/Desktop/GitTest (hot-fix)
$ git commit -m "hot-fix分支的第一次提交"
[hot-fix 0b69f78] hot-fix分支的第一次提交
 1 file changed, 2 insertions(+), 2 deletions(-)

Jason@JASON MINGW64 ~/Desktop/GitTest (hot-fix)
$ git status
On branch hot-fix
nothing to commit, working tree clean

Jason@JASON MINGW64 ~/Desktop/GitTest (hot-fix)
$ cat hello.txt
hello world!hello Git!hot-fix分支
hello world!hello Git!
hello world!hello Git!
hello world!hello Git!
hello world!hello Git!
hello world!hello Git!
hello world!hello Git!

image-20210904195238640

3.2.3 查看Git本地文件夹

  • hello.txt本地文件已修改
image-20210904195323641
  • 查看HEAD文件
image-20210904195554503
  • 查看heads文件
image-20210904195820300
  • 对比版本号
image-20210904200119689

3.4 ==合并分支==

3.4.1 基本语法

git merge 分支名

3.4.2 合并演练

Jason@JASON MINGW64 ~/Desktop/GitTest (hot-fix)
$ git reflog
0b69f78 (HEAD -> hot-fix) HEAD@{0}: commit: hot-fix分支的第一次提交
761404b (master) HEAD@{1}: checkout: moving from master to hot-fix
761404b (master) HEAD@{2}: reset: moving to 761404b
a2b8091 HEAD@{3}: reset: moving to a2b8091
761404b (master) HEAD@{4}: commit: third commit
a2b8091 HEAD@{5}: commit: second commit
162913e HEAD@{6}: commit (initial): first commit

Jason@JASON MINGW64 ~/Desktop/GitTest (hot-fix)
$ git checkout master
Switched to branch 'master'

Jason@JASON MINGW64 ~/Desktop/GitTest (master)
$ cat hello.txt
hello world!hello Git!1111
hello world!hello Git!2222
hello world!hello Git!
hello world!hello Git!
hello world!hello Git!
hello world!hello Git!
hello world!hello Git!


Jason@JASON MINGW64 ~/Desktop/GitTest (master)
$ git merge hot-fix
Updating 761404b..0b69f78
Fast-forward
 hello.txt | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

Jason@JASON MINGW64 ~/Desktop/GitTest (master)
$ cat hello.txt
hello world!hello Git!hot-fix分支
hello world!hello Git!
hello world!hello Git!
hello world!hello Git!
hello world!hello Git!
hello world!hello Git!
hello world!hello Git!

image-20210904203031465

3.4.3 ==合并分支产生冲突==

  • master分支上修改hello.txt文件,并提交代码
Jason@JASON MINGW64 ~/Desktop/GitTest (master)
$ vim hello.txt

Jason@JASON MINGW64 ~/Desktop/GitTest (master)
$ cat hello.txt
'hello world!hello Git!
hello world!hello Git!
hello world!hello Git!
hello world!hello Git!
hello world!hello Git!
hello world!hello Git! master分支
hello world!hello Git!


Jason@JASON MINGW64 ~/Desktop/GitTest (master)
$ git add hello.txt

Jason@JASON MINGW64 ~/Desktop/GitTest (master)
$ git commit -m "master分支合并(演示冲突)"
[master 76ae418] master分支合并(演示冲突)
 1 file changed, 1 insertion(+), 1 deletion(-)


Jason@JASON MINGW64 ~/Desktop/GitTest (master)
$ git status
On branch master
nothing to commit, working tree clean

Jason@JASON MINGW64 ~/Desktop/GitTest (master)
$ git reflog
76ae418 (HEAD -> master) HEAD@{0}: commit: master分支合并(演示冲突)
0b69f78 (hot-fix) HEAD@{1}: merge hot-fix: Fast-forward
761404b HEAD@{2}: checkout: moving from hot-fix to master
0b69f78 (hot-fix) HEAD@{3}: commit: hot-fix分支的第一次提交
761404b HEAD@{4}: checkout: moving from master to hot-fix
761404b HEAD@{5}: reset: moving to 761404b
a2b8091 HEAD@{6}: reset: moving to a2b8091
761404b HEAD@{7}: commit: third commit
a2b8091 HEAD@{8}: commit: second commit
162913e HEAD@{9}: commit (initial): first commit

image-20210904211631322
  • hot-fix分支修改hello.txt文件,并提交代码
Jason@JASON MINGW64 ~/Desktop/GitTest (master)
$ git checkout hot-fix
Switched to branch 'hot-fix'

Jason@JASON MINGW64 ~/Desktop/GitTest (hot-fix)
$ vim hello.txt

Jason@JASON MINGW64 ~/Desktop/GitTest (hot-fix)
$ cat hello.txt
hello world!hello Git!
hello world!hello Git!
hello world!hello Git!
hello world!hello Git!
hello world!hello Git!
hello world!hello Git!
hello world!hello Git!   hot-fix分支合并


Jason@JASON MINGW64 ~/Desktop/GitTest (hot-fix)
$ git add hello.txt

Jason@JASON MINGW64 ~/Desktop/GitTest (hot-fix)
$ git commit -m "hot-fix合并分支(演示冲突)"
[hot-fix 49742aa] hot-fix合并分支(演示冲突)
 1 file changed, 1 insertion(+), 1 deletion(-)

Jason@JASON MINGW64 ~/Desktop/GitTest (hot-fix)
$ git reflog
49742aa (HEAD -> hot-fix) HEAD@{0}: commit: hot-fix合并分支(演示冲突)
0b69f78 HEAD@{1}: checkout: moving from master to hot-fix
76ae418 (master) HEAD@{2}: commit: master分支合并(演示冲突)
0b69f78 HEAD@{3}: merge hot-fix: Fast-forward
761404b HEAD@{4}: checkout: moving from hot-fix to master
0b69f78 HEAD@{5}: commit: hot-fix分支的第一次提交
761404b HEAD@{6}: checkout: moving from master to hot-fix
761404b HEAD@{7}: reset: moving to 761404b
a2b8091 HEAD@{8}: reset: moving to a2b8091
761404b HEAD@{9}: commit: third commit
a2b8091 HEAD@{10}: commit: second commit
162913e HEAD@{11}: commit (initial): first commit

image-20210904212318706
  • 合并冲突
Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (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.

3.4.4 产生冲突

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

Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master|MERGING)
$ cat hello.txt
hello git! hello atguigu! 2222222222222
hello git! hello atguigu! 3333333333333
hello git! hello atguigu!
hello git! hello atguigu!
hello git! hello atguigu!
hello git! hello atguigu!
hello git! hello atguigu!
hello git! hello atguigu!
hello git! hello atguigu!
hello git! hello atguigu!
hello git! hello atguigu!
hello git! hello atguigu!
hello git! hello atguigu!
hello git! hello atguigu!
<<<<<<< HEAD
hello git! hello atguigu! master test
hello git! hello atguigu!
=======
hello git! hello atguigu!
hello git! hello atguigu! hot-fix test
>>>>>>> hot-fix

3.4.5 ==冲突产生的原因:==

<u>合并分支时,两个分支在 同一个文件的同一个位置有两套完全不同的修改。Git 无法替我们决定使用哪一个。必须 人为决定新代码内容。</u>
==查看状态(检测到有文件有两处修改)==

Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (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")

3.4.6 解决冲突

3.4.6.1 编辑有冲突的文件,删除特殊符号,决定要使用的内容
特殊符号:
<<<<<<< HEAD 

当前分支的代码 

======= 

合并过来的代码 

>>>>>>> hot-fix
hello git! hello atguigu! 2222222222222
hello git! hello atguigu! 3333333333333
hello git! hello atguigu!
hello git! hello atguigu!
hello git! hello atguigu!
hello git! hello atguigu!
hello git! hello atguigu!
hello git! hello atguigu!
hello git! hello atguigu!
hello git! hello atguigu!
hello git! hello atguigu!
hello git! hello atguigu!
hello git! hello atguigu!
hello git! hello atguigu!
hello git! hello atguigu! master test
hello git! hello atguigu! hot-fix test
3.4.6.2 添加到暂存区
Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master|MERGING)
 git add hello.txt
3.4.6.3 ==执行提交(注意:此时使用 git commit 命令时 不能带文件名)==
Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master|MERGING)
 git commit -m "merge hot-fix"
[master 69ff88d] merge hot-fix
--发现后面 MERGING 消失,变为正常

Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master)

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

image-20210904214245617
  • master、hot-fix 其实都是指向具体版本记录的指针。当前所在的分支,其实是由 HEAD决定的。所以创建分支的本质就是多创建一个指针。
  • HEAD 如果指向 master,那么我们现在就在 master 分支上。
  • HEAD 如果执行 hotfix,那么我们现在就在 hotfix 分支上。
  • 所以切换分支的本质就是移动 HEAD 指针。
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 219,270评论 6 508
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,489评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 165,630评论 0 356
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,906评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,928评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,718评论 1 305
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,442评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,345评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,802评论 1 317
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,984评论 3 337
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,117评论 1 351
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,810评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,462评论 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,011评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,139评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,377评论 3 373
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,060评论 2 355

推荐阅读更多精彩内容