目錄
1.?線性分支結構
2. 分叉與合并結構
3. 分支與標簽的關系
4.?并行開發與分支管理策略
測試(本機系統為Rocky_linux9.4)
合并失敗解決
刪除分支
?刪除本地分支
刪除遠程分支
Git 中的分支結構是版本控制中非常重要的概念之一,它描述了項目中不同提交的組織方式和分支之間的關系。以下是Git 分支結構及其解釋:
1.?線性分支結構
- 最簡單的分支結構是線性的,每次提交都是在前一次提交的基礎上進行的。這種結構沒有分叉或合并,所有的提交都在同一個主線上。
A --- B --- C --- D (master)
- 上面的示例中,
A
、B
、C
、D
表示不同的提交,master
分支順序地指向每個新的提交。
2. 分叉與合并結構
- 在實際項目中,通常會存在多個并行開發的分支,這些分支可以同時在不同的特性或修復上工作,然后將它們合并回主分支或其他分支。
/--- E --- G (feature1)
A --- B --- C --- D\--- F --- H (feature2)
- ?在上面的示例中,
feature1
和feature2
是從B
提交分叉出來的分支。E
和F
是各自分支上的提交,G
和H
是將特性分支合并回主分支后的提交。
3. 分支與標簽的關系
- 標簽(Tag)通常用于標記重要的版本發布點或里程碑,它們可以指向任意的提交,不一定是分支的頭部。
A --- B --- C --- D (master, v1.0)\E --- F (v2.0)
- 在這個示例中,
v1.0
標簽指向提交D
,表示v1.0
版本的發布點。而v2.0
標簽指向提交F
,表示v2.0
版本的發布點。
4.?并行開發與分支管理策略
? ? ? ?在團隊協作中,良好的分支管理策略可以有效地管理并行開發和版本控制。常見的策略包括使用主分支(如 master
)作為穩定版本的發布線,使用特性分支來開發新功能或修復 bug,并通過合并操作將它們整合回主分支。
測試(本機系統為Rocky_linux9.4)
分支切換
[root@tty02 tty]# git branch newrain #創建一個分支
[root@tty02 tty]# git branch
* (HEAD detached at ugo)masternewrain
[root@tty02 tty]# git checkout newrain #切換到這個分支
Switched to branch 'newrain'
[root@tty02 tty]# git branchmaster
* newrain
?在newrain分支進行修改
[root@tty02 tty]# ll
total 0
-rw-r--r-- 1 root root 0 Jul 9 22:37 tty
[root@tty02 tty]# echo "1901" >> tty
[root@tty02 tty]# cat tty
1901
[root@tty02 tty]# git add .
[root@tty02 tty]# git commit -m '1901']
[newrain 96638c2] 1901]1 file changed, 1 insertion(+)
[root@tty02 tty]# git status
On branch newrain
nothing to commit, working tree clean#此時位于分支 newrain 無文件要提交,干凈的工作區
回到master分支
[root@tty02 tty]# git checkout master
Switched to branch 'master'
Your branch is based on 'origin/master', but the upstream is gone.(use "git branch --unset-upstream" to fixup)
[root@tty02 tty]# git branch
* masternewrain
[root@tty02 tty]# ll
total 0
-rw-r--r-- 1 root root 0 Jul 9 23:39 tty
[root@tty02 tty]# cat tty #此時在master分支上看不到內容
[root@tty02 tty]# git log -1
commit d2470f142c7721ebbff8a3b8f5f7752ecbae67c8 (HEAD -> master, tag: v2.0, tag: v1.0, tag: ugo)
Author: Your Name <you@example.com>
Date: Tue Jul 9 22:35:17 2024 +0800commit
合并代碼
[root@tty02 tty]# git merge newrain #合并newrain到當前master分支
Updating d2470f1..96638c2
Fast-forwardtty | 1 +1 file changed, 1 insertion(+)[root@tty02 tty]# git status
On branch master
Your branch is based on 'origin/master', but the upstream is gone.(use "git branch --unset-upstream" to fixup)nothing to commit, working tree clean[root@tty02 tty]# cat tty
1901
合并后即可看到內容
合并失敗解決
模擬沖突,在文件的同一行做不同修改
在master分支進行修改?
[root@tty02 tty]# cat tty
1901
[root@tty02 tty]# echo '1901-git' > tty
[root@tty02 tty]# git add .
[root@tty02 tty]# git commit -m 'newrain 1901-git'
[master d343709] newrain 1901-git1 file changed, 1 insertion(+), 1 deletion(-)
?切換到newrain分支
[root@tty02 tty]# git checkout newrain
Switched to branch 'newrain'[root@tty02 tty]# git branchmaster
* newrain[root@tty02 tty]# cat tty
1901[root@tty02 tty]# echo 'newrain' >> tty
[root@tty02 tty]# git add .
[root@tty02 tty]# git commit -m '1901-git-check'
[newrain d32a6f0] 1901-git-check1 file changed, 1 insertion(+)
回到master分區,進行合并,出現沖突
[root@tty02 tty]# git checkout master
Switched to branch 'master'
Your branch is based on 'origin/master', but the upstream is gone.(use "git branch --unset-upstream" to fixup)
[root@tty02 tty]# git branch
* masternewrain[root@tty02 tty]# git merge newrain #此時合并遇到了沖突
Auto-merging tty
CONFLICT (content): Merge conflict in tty
Automatic merge failed; fix conflicts and then commit the result.
解決沖突
[root@tty02 tty]# cat tty #查看該文件
<<<<<<< HEAD
1901-git
=======
1901
newrain
>>>>>>> newrain
解釋:
<<<<<<< HEAD
// 當前分支的修改內容
=======
// 待合并分支的修改內容
>>>>>>> newrain
?編輯該沖突文件,手動刪除不需要的部分即可
[root@tty02 tty]# vim tty #刪除后見下圖,下圖就是我要保留的內容
[root@tty02 tty]# git add .
[root@tty02 tty]# git commit -m "解決合并沖突"
[master 47f3b39] 解決合并沖突
?此時就已經解決該沖突。
刪除分支
因為之前已經合并了newrain分支,所以現在看到它在列表中。 在這個列表中分支名字前沒有 * 號的分支通常可以使用 git branch -d 刪除掉;剛剛已經將它們的工作整合到了另一個分支,所以并不會失去任何東西。查看所有包含未合并工作的分支,可以運行 git branch --no-merged
如果真的想要刪除分支并丟掉那些工作,如同幫助信息里所指出的,可以使用 -D 選項強制刪除它。
?刪除本地分支
[root@tty02 tty]# git branch #查看分支
* masternewrain[root@tty02 tty]# git branch -d newrain #刪除這個分支
Deleted branch newrain (was d32a6f0).[root@tty02 tty]# git branch #此時就沒有了
* master
刪除遠程分支
如果需要刪除遠程倉庫中的分支,可以使用 git push
命令和 --delete
選項:
git push origin --delete <branch-name>
這樣就完成了刪除分支的操作。在執行任何刪除操作之前,確保理解并確認刪除的分支不再需要,并且對刪除的后果有清晰的了解。