Git筆記 branch / merge


觀念

假設以樹枝當作開發的流程,那commit process相當於枝條
而branch相當於指向枝條的樹葉

commit process

當我們要多人協作的時候,需要可以去回溯我們的workflows
也就是所謂的commit process (可以用git log查看)
而這些commit process組成的分支,我們稱為branch

(commit process 就是一堆commit串起來的)

branch

為指向某個commits點的pointer,接連著其他相關的commits點
所以當建立新branch,其實也就是新建一個pointer\
對整體repo來說並沒有麼影響

那當我們使用git init在檔案中增加.git檔
此時我們是增加一個名為main/master的branch

通常github初始repo branch ==> main
本地repogit init ==> master
當然這些branch名稱都可以再重新命名

Head

使用Head事告訴我們目前checking out的commit點為何
這個commit點與Head目前所在的branch相關
Head也可以切換到其他branch來查看其他commit點

當我們使用git log可以看到 commit 05as..3e2 (HEAD -> master)
表示Head為目前master指向的commit

Merging branches

Fast Forward Merge:

當我們想把master指向的commit和new_branch同步

git switch master

git merge new_branch

Git Merge:


git merge new_branch
這種情況下會產生一個新的commit作為merge點
通常我們要為這個新的commit點命名

練習

新增branch / git switch

1.git init , git add ,git commit
在初始檔案裡面產生commit point


file.txt:
line 1
line 2
line 3

  1. git branch new_branch 增加一個branch

    目前Head還是在master

git branch 查看當前所有branch
git branch [name] 創立新的branch

  1. git add ,git commit , git log 查看master branch的commit狀況

file.txt:
line 1
line 2
line 3
in master! (new add)

git add file.txt git commit -m "in master"
git log查看

可以看到Head往master的下一個commit point跑!

4.git switch new_branch , 從branch master切到new_branch


可以看到new_branch是指向一開始創立branch時候的commit point

5.git add, git commit, git log 在new_branch上面增加新commit point

file.txt:
line 1
line 2
line 3
這我們再加上 in new_branch! git status

git add file.txt git commit -m "in new_branch"
git log

6.在切回git switch master, 再次切回 branch master

rename / delete branch

接下來切回new_branch ,修改file.txt:
line 1
line 2
line 3
in new_branch
experiment (新增)

git add file.txt , git commit -m "add experiment"

創立新的branch , git branch experiment

接下來我們要將branch experiment刪除

(1)git branch -m [newName] 將當下在的branch名稱重新命名
experiment --> please_delete

(2)切回master對branch please_delete進行刪除

Merge

Fast Forward Merge:


file.txt:
one line


file.txt:
(1)
one line
add a new_branch

(2)
one line
add a new_branch
againg add a new_branch

現在我們將master和new_branch merge

branch master file.txt:
one line
add a new_branch
againg add a new_branch

Git Merge:

我們切回master新增branch b1
master: file.txt
b1: file.txt

接下來master新增master.file,處理後切到b1
b1新增b1.txt
-->b1和master merge git merge master 沒有衝突

新的commit點為兩者檔案的交集:
file.txt
master.txt
b1.txt

Merge with comflict

回到master merge b1
新建branch b2

(1)master file.txt 新增一行 master in the house!
(2)切去b2 file.txt 新增一行 b2 in the house!
(3)切回master ,merge b2
合併的時候會發生衝突:

上面有一些選項可以讓你決定怎麼處理這個衝突
處理完後再add,commit存下解決衝突後的版本

Git diff

假設說我們再改好檔案就去睡覺 忘了要add,commit
那我怎麼比對剛剛修改跟原本commit過版本的差異
比對不同版本a,b的檔案差異
git diff branch1 branch2

+/-分別代表來自不同的版本的差異








你可能感興趣的文章

認識資料庫的推薦閱讀

認識資料庫的推薦閱讀

EDA工作站建置(CENTOS 8)

EDA工作站建置(CENTOS 8)

安裝 vim-devicons 檔案圖示

安裝 vim-devicons 檔案圖示






留言討論