1. 程式人生 > 其它 >(四)Git分支操作

(四)Git分支操作

一、什麼是分支

在版本控制過程中,同時推進多個任務,為每個任務,我們就可以建立每個任務的單獨分支。使用分支意味著程式設計師可以把自己的工作從開發主線上分離開來,開發自己分支的時候,不會影響主線分支的執行。對於初學者而言,分支可以簡單理解為副本,一個分支就是一個單獨的副本。(分支底層其實也是指標的引用)

二、分支的好處

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

三、分支操作

3.1 檢視分支

1、基本語法

git branch -v

2、案例操作

Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master)
$ git branch -v
* master 087a1a7 my third commit (*代表當前所在的分割槽)

3.2 建立分支

1、基本語法

git branch 分支名

2、案例操作

Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master)
$ git branch hot-fix
Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master)
$ git branch -v
hot-fix 087a1a7 my third commit (剛建立的新的分支,並將主分支 master的內容複製了一份)
* master 087a1a7 my third commit

3.3 修改分支

--在 maste 分支上做修改
Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master)
$ vim hello.txt
--新增暫存區
Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master)
$ git add hello.txt
--提交本地庫
Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master)
$ git commit -m "my forth commit" hello.txt
[master f363b4c] my forth commit
1 file changed, 1 insertion(+), 1 deletion(-)
--檢視分支
Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master)
$ git branch -v
hot-fix 087a1a7 my third commit (hot-fix 分支並未做任何改變)
* master f363b4c my forth commit (當前 master 分支已更新為最新一次提交
的版本)
--檢視 master 分支上的檔案內容
Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master)
$ 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!
hello git! hello atguigu! master test
hello git! hello atguigu!

3.4 切換分支

1、基本語法

git checkout 分支名

2、案例操作

Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master)
$ git checkout hot-fix
Switched to branch 'hot-fix'
--發現當先分支已由 master 改為 hot-fix
Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (hot-fix)
$
--檢視 hot-fix 分支上的檔案內容發現與 master 分支上的內容不同
Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (hot-fix)
$ 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!
hello git! hello atguigu!
hello git! hello atguigu!
--在 hot-fix 分支上做修改
Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (hot-fix)
$ 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!
hello git! hello atguigu!
hello git! hello atguigu! hot-fix test
--新增暫存區
Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (hot-fix)
$ git add hello.txt
--提交本地庫
Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (hot-fix)
$ git commit -m "hot-fix commit" hello.txt

3.5 合併分支

1、基本語法

git merge 分支名

2、案例操作

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.6 產生衝突

衝突產生的表現:後面狀態為 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

衝突產生的原因:
合併分支時,兩個分支在 同一個檔案的同一個位置有兩套完全不同的修改。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 <file>..." to mark resolution)
both modified: hello.txt
no changes added to commit (use "git add" and/or "git commit -a")

3.7 解決衝突

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

2)新增到暫存區

Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master|MERGING)
$ git add hello.txt

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)
$

四、建立分支和切換分支圖解


master、hot-fix 其實都是指向具體版本記錄的指標。當前所在的分支,其實是由 HEAD決定的。所以建立分支的本質就是多建立一個指標。
HEAD 如果指向 master,那麼我們現在就在 master 分支上。
HEAD 如果執行 hotfix,那麼我們現在就在 hotfix 分支上。
所以切換分支的本質就是移動 HEAD 指標。