04.git分支操作
一.什么是分支?
在版本控制過程中,同時推進(jìn)多個任務(wù)衷笋,為每個任務(wù)逸月,我們就可以創(chuàng)建每個任務(wù)的單獨(dú)分支。使用分支意味著程序員可以把自己的工作從開發(fā)主線上分離開來纪隙,開發(fā)自己分支的時候赊豌,不會影響主線分支的運(yùn)行。對于初學(xué)者而言瘫拣,分支可以簡單理解為副本亿絮,一個分支就是一個單獨(dú)的副本。(分支底層其實(shí)也是指針的引用)
二.分支的好處
同時并行推進(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
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)
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!
3.2.3 查看Git本地文件夾
- hello.txt本地文件已修改
- 查看HEAD文件
- 查看heads文件
- 對比版本號
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!
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
- 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
- 合并沖突
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)建分支和切換分支圖解
- 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 指針。