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 ..." to update what will be committed)
  (use "git restore ..." 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 ..." to update what will be committed)
  (use "git restore ..." 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 ..." 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 ==冲突产生的原因:==

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

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 ..." 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 指针。

你可能感兴趣的:(04.git分支操作)