04.git分支操作

04.git分支操作

image-20210904191135798

一.什么是分支?

     在版本控制過程中,同時推進多個任務,為每個任務,我們就可以創(chuàng)建每個任務的單獨分支。使用分支意味著程序員可以把自己的工作從開發(fā)主線上分離開來,開發(fā)自己分支的時候,不會影響主線分支的運行。對于初學者而言,分支可以簡單理解為副本,一個分支就是一個單獨的副本。(分支底層其實也是指針的引用)
image-20210904191229879

二.分支的好處

同時并行推進多個功能開發(fā),提高開發(fā)效率。
各個分支在開發(fā)過程中,如果某一個分支開發(fā)失敗,不會對其他分支有任何影響。失敗的分支刪除重新開始即可。

三.分支的操作

命令名稱 作用
git branch 分支名 創(chuàng)建分支
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(創(chuàng)建分支)

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

3.2 創(chuàng)建分支

git branch 分支名

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

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 添加暫存區(qū),提交到本地庫

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 ==合并分支產(chǎn)生沖突==

  • 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 產(chǎn)生沖突

沖突產(chǎn)生的表現(xiàn):后面狀態(tài)為 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 ==沖突產(chǎn)生的原因:==

<u>合并分支時,兩個分支在 同一個文件的同一個位置有兩套完全不同的修改。Git 無法替我們決定使用哪一個。必須 人為決定新代碼內(nèi)容。</u>
==查看狀態(tài)(檢測到有文件有兩處修改)==

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 編輯有沖突的文件,刪除特殊符號,決定要使用的內(nèi)容
特殊符號:
<<<<<<< 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 添加到暫存區(qū)
Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master|MERGING)
 git add hello.txt
3.4.6.3 ==執(zhí)行提交(注意:此時使用 git commit 命令時 不能帶文件名)==
Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master|MERGING)
 git commit -m "merge hot-fix"
[master 69ff88d] merge hot-fix
--發(fā)現(xiàn)后面 MERGING 消失,變?yōu)檎?
Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master)

3.5 創(chuàng)建分支和切換分支圖解

image-20210904214245617
  • master、hot-fix 其實都是指向具體版本記錄的指針。當前所在的分支,其實是由 HEAD決定的。所以創(chuàng)建分支的本質(zhì)就是多創(chuàng)建一個指針。
  • HEAD 如果指向 master,那么我們現(xiàn)在就在 master 分支上。
  • HEAD 如果執(zhí)行 hotfix,那么我們現(xiàn)在就在 hotfix 分支上。
  • 所以切換分支的本質(zhì)就是移動 HEAD 指針。
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容