1. 程式人生 > 其它 >git分支操作

git分支操作

目錄

什麼是分支

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

分支的好處

  • 同時並行推進多個功能開發,提高開發效率。

  • 各個分支在開發過程中,如果某一個分支開發失敗,不會對其他分支有任何影響。失敗的分支刪除重新開始即可。

分支的操作

命令名稱 作用
git branch 分支名 建立分支
git branch -v 檢視分支
git checkout 分支名 切換分支
git merge 分支名 把指定的分支合併到當前分支上

檢視分支

  1. 基本語法
    git branch -v 
    
  2. 案例實操

建立分支

  1. 基本語法
git branch 分支名
  1. 案例實操

修改分支


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

切換分支

  1. 基本語法
git checkout 分支名

2)案例實操

—————————————————————————————
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

合併分支

  1. 基本語法
git merge 分支名

2.案例實操
在 master 分支上合併 hot-fix 分支

產生衝突

衝突產生的表現:後面狀態為 MERGING
Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master|MERGING)

—————————————————————————————

衝突產生的原因:

合併分支時,兩個分支在同一個檔案的同一個位置有兩套完全不同的修改。Git 無法替我們決定使用哪一個。必須人為決定新程式碼內容。
檢視狀態(檢測到有檔案有兩處修改)

解決衝突

1)編輯有衝突的檔案,刪除特殊符號,決定要使用的內容

特殊符號:<<<<<<< HEAD 當前分支的程式碼 ======= 合併過來的程式碼 >>>>>>> hot-fix

2)新增到暫存區
3)執行提交(注意:此時使用 git commit 命令時不能帶檔名)

建立分支和切換分支圖解

master、hot-fix 其實都是指向具體版本記錄的指標。當前所在的分支,其實是由 HEAD

決定的。所以建立分支的本質就是多建立一個指標。

HEAD 如果指向 master,那麼我們現在就在master 分支上。

HEAD 如果執行 hotfix,那麼我們現在就在hotfix 分支上。

所以切換分支的本質就是移動HEAD 指標。