04.git分支操作

04.git分支操作

image-20210904191135798

一.什么是分支?

     在版本控制過程中,同時推進(jìn)多個任務(wù)衷笋,為每個任務(wù)逸月,我們就可以創(chuàng)建每個任務(wù)的單獨(dú)分支。使用分支意味著程序員可以把自己的工作從開發(fā)主線上分離開來纪隙,開發(fā)自己分支的時候赊豌,不會影響主線分支的運(yùn)行。對于初學(xué)者而言瘫拣,分支可以簡單理解為副本亿絮,一個分支就是一個單獨(dú)的副本。(分支底層其實(shí)也是指針的引用)
image-20210904191229879

二.分支的好處

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

三.分支的操作

命令名稱 作用
git branch 分支名 創(chuàng)建分支
git branch -v 查看分支
git checkout 分支名 切換分支
git merge 分支名 把指定的分支合并到當(dāng)前分支上

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 

當(dāng)前分支的代碼 

======= 

合并過來的代碼 

>>>>>>> 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 其實(shí)都是指向具體版本記錄的指針架曹。當(dāng)前所在的分支隘冲,其實(shí)是由 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)系作者
  • 序言:七十年代末脚粟,一起剝皮案震驚了整個濱河市靡砌,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌珊楼,老刑警劉巖,帶你破解...
    沈念sama閱讀 206,839評論 6 482
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件度液,死亡現(xiàn)場離奇詭異厕宗,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)堕担,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,543評論 2 382
  • 文/潘曉璐 我一進(jìn)店門已慢,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人霹购,你說我怎么就攤上這事佑惠。” “怎么了齐疙?”我有些...
    開封第一講書人閱讀 153,116評論 0 344
  • 文/不壞的土叔 我叫張陵膜楷,是天一觀的道長。 經(jīng)常有香客問我贞奋,道長赌厅,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 55,371評論 1 279
  • 正文 為了忘掉前任轿塔,我火速辦了婚禮特愿,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘勾缭。我一直安慰自己揍障,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 64,384評論 5 374
  • 文/花漫 我一把揭開白布俩由。 她就那樣靜靜地躺著毒嫡,像睡著了一般。 火紅的嫁衣襯著肌膚如雪采驻。 梳的紋絲不亂的頭發(fā)上审胚,一...
    開封第一講書人閱讀 49,111評論 1 285
  • 那天匈勋,我揣著相機(jī)與錄音,去河邊找鬼膳叨。 笑死洽洁,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的菲嘴。 我是一名探鬼主播饿自,決...
    沈念sama閱讀 38,416評論 3 400
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼龄坪!你這毒婦竟也來了昭雌?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 37,053評論 0 259
  • 序言:老撾萬榮一對情侶失蹤健田,失蹤者是張志新(化名)和其女友劉穎烛卧,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體妓局,經(jīng)...
    沈念sama閱讀 43,558評論 1 300
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡总放,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,007評論 2 325
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了好爬。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片局雄。...
    茶點(diǎn)故事閱讀 38,117評論 1 334
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖存炮,靈堂內(nèi)的尸體忽然破棺而出炬搭,到底是詐尸還是另有隱情,我是刑警寧澤穆桂,帶...
    沈念sama閱讀 33,756評論 4 324
  • 正文 年R本政府宣布宫盔,位于F島的核電站,受9級特大地震影響充尉,放射性物質(zhì)發(fā)生泄漏飘言。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,324評論 3 307
  • 文/蒙蒙 一驼侠、第九天 我趴在偏房一處隱蔽的房頂上張望姿鸿。 院中可真熱鬧,春花似錦倒源、人聲如沸苛预。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,315評論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽热某。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間昔馋,已是汗流浹背筹吐。 一陣腳步聲響...
    開封第一講書人閱讀 31,539評論 1 262
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留秘遏,地道東北人丘薛。 一個月前我還...
    沈念sama閱讀 45,578評論 2 355
  • 正文 我出身青樓,卻偏偏與公主長得像邦危,于是被迫代替她去往敵國和親洋侨。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 42,877評論 2 345

推薦閱讀更多精彩內(nèi)容