1. 程式人生 > >git的分支管理策略

git的分支管理策略

分支在實際中的作用

假設你準備開發一個新功能,但是需要兩週才能完成,第一週你寫了50%的程式碼,如果立刻提交,由於程式碼還沒寫完,不完整的程式碼庫會導致別人不能幹活了。如果等程式碼全部寫完再一次提交,又存在丟失每天進度的巨大風險。現在有了分支,就不用怕了。你建立了一個屬於你自己的分支,別人看不到,還繼續在原來的分支上正常工作,而你在自己的分支上幹活,想提交就提交,直到開發完畢後,再一次性合併到原來的分支上,這樣,既安全,又不影響別人工作。

分支的原理描述

在版本回退裡,每次提交,Git都把它們串成一條時間線,這條時間線就是一個分支。開始的時候,只有一條時間線,在Git裡,這個分支叫主分支,即master

分支。HEAD嚴格來說不是指向提交,而是指向mastermaster才是指向提交的,所以,HEAD指向的就是當前分支。

一開始的時候,master分支是一條線,Git用master指向最新的提交,再用HEAD指向master,就能確定當前分支,以及當前分支的提交點.

每次提交,master分支都會向前移動一步,這樣,隨著你不斷提交,master分支的線也越來越長:

當我們建立新的分支,例如dev時,Git新建了一個指標叫dev,指向master相同的提交,再把HEAD指向dev,就表示當前分支在dev上.

這樣一個dev的分支就建成了,不過,從現在開始,對工作區的修改和提交就是針對dev

分支了,比如新提交一次後,dev指標往前移動一步,而master指標不變。

我們在dev上的工作完成了,就可以把dev合併到master上。最簡單的方法,就是直接把master指向dev的當前提交,就完成了合併。合併完分支後,就可以刪除無用的dev分支。刪除dev分支就是把dev指標給刪掉,刪掉後,我們就剩下了一條master分支。

分支的建立與刪除

[email protected]:~/joe/learngit$ ls
abc.c  readme.txt
[email protected]:~/joe/learngit$ git status
位於分支 master        
//當前位於主分支 您的分支與上游分支 'origin/master' 一致。 無檔案要提交,乾淨的工作區 [email protected]:~/joe/learngit$
git checkout -b dev
    //建立一個分支並切換到當前分支
切換到一個新分支 'dev'
[email protected]:~/joe/learngit$ 
git branch
    //檢視所有分支,(當前的分支前會有*號)
* dev
  master
[email protected]:~/joe/learngit$ ls
abc.c  readme.txt
[email protected]:~/joe/learngit$ vi readme.txt     //在dev分支下修改檔案
[email protected]:~/joe/learngit$ 
git status
    //檢視狀態,是在dev分支下
位於分支 dev
尚未暫存以備提交的變更:
  (使用 "git add <file>..." 更新要提交的內容)
  (使用 "git checkout -- <file>..." 丟棄工作區的改動)

    修改:     readme.txt

修改尚未加入提交(使用 "git add" 和/或 "git commit -a"[email protected]:~/joe/learngit$ git add readme.txt 
[email protected]:~/joe/learngit$ 
git commit -m "new dev branch"

[dev 9409b92] new dev branch
 1 file changed, 2 insertions(+), 1 deletion(-)
[email protected]:~/joe/learngit$ 
git checkout master
    //將dev分支下的工作提交以後,切換到主分支
切換到分支 'master'
您的分支與上游分支 'origin/master' 一致。
[email protected]:~/joe/learngit$ 
cat readme.txt
         //主分支下檢視檔案發現檔案內容並沒有修改
Git is a distributed version control system
Git is free software distributed under the
Are you ok?
Yes,i am fine.
What about you?
[email protected]:~/joe/learngit$ 
git merge dev
        //dev分支和master分支合併
更新 f10fe58..9409b92
Fast
-
forward//Fast-forward資訊,Git告訴我們,這次合併是“快進模式”,也就是直接把master指向dev的當前提交,所以合併速度非常快。當然,也不是每次合併都能Fast-forward,就如下面的衝突
 readme.txt | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
[email protected]:~/joe/learngit$ 
cat readme.txt
         //合併以後,再次檢視,檔案內容已經修改
Git is a distributed version control system
Git is free software distributed under the
Are you ok?
Yes,i am fine.
What about you
Create a branch is so quick.
[email protected]:~/joe/learngit$ 
git branch -d dev
    //刪除分株
已刪除分支 dev(曾為 9409b92)。
[email protected]:~/joe/learngit$ git branch
* master
//git checkout命令加上-b引數表示建立並切換,相當於以下兩條命令:

$ git branch dev    //建立分支
$ git checkout dev    //切換分支
Switched to branch 'dev'

分支的衝突

[email protected]:~/joe/learngit$ git status
位於分支 master
您的分支領先 'origin/master'1 個提交。
  (使用 "git push" 來發布您的本地提交)
無檔案要提交,乾淨的工作區
[email protected]:~/joe/learngit$ 
git checkout -b bran
    //新建bran分支,修改檔案內容並提交
切換到一個新分支 'bran'
[email protected]:~/joe/learngit$ vi abc.c 
[email protected]:~/joe/learngit$ cat abc.c 
I am a new branch
[email protected]:~/joe/learngit$ git add abc.c 
[email protected]:~/joe/learngit$ 
git commit -m "new bran"

[bran 5aa28d8] new bran
 1 file changed, 1 insertion(+), 1 deletion(-)
[email protected]:~/joe/learngit$ 
git checkout master
    //切換回主分支,修改同一個檔案,並提交
切換到分支 'master'
您的分支領先 'origin/master'1 個提交。
  (使用 "git push" 來發布您的本地提交)
[email protected]:~/joe/learngit$ vi abc.c 
[email protected]:~/joe/learngit$ cat abc.c 
I am not a branch
[email protected]:~/joe/learngit$ git add abc.c 
[email protected]:~/joe/learngit$ git 
commit -m "not a branch"

[master fe42f3e] not a branch
 1 file changed, 1 insertion(+), 1 deletion(-)
[email protected]:~/joe/learngit$ 
git merge bran
    //此時合併分支的時候,出現了衝突(2個分支都對檔案做了修改,那麼合併的時候應該合併哪一個呢?此時需要手動解決以後,才可以合併。)
自動合併 abc.c
衝突(內容):合併衝突於 abc.c
自動合併失敗,修正衝突然後提交修正的結果。
[email protected]:~/joe/learngit$ git status
位於分支 master
您的分支領先 'origin/master'2 個提交。
  (使用 "git push" 來發布您的本地提交)
您有尚未合併的路徑。
  (解決衝突並執行 "git commit"未合併的路徑:
  (使用 "git add <file>..." 標記解決方案)

    雙方修改:   abc.c

修改尚未加入提交(使用 "git add" 和/或 "git commit -a"[email protected]:~/joe/learngit$ 
cat abc.c
     //檢視檔案內容
<<<<<<< HEAD
I am not a branch
=======
I am a new branch
>>>>>>> bran
//Git用<<<<<<<,=======,>>>>>>>標記出不同分支的內容
[email protected]:~/joe/learngit$ 
vi abc.c
     //將其檔案內容修改統一後,提交
[email protected]:~/joe/learngit$ cat abc.c 
I am not a branch
[email protected]:~/joe/learngit$ git add abc.c 
[email protected]:~/joe/learngit$
git commit -m "conflict ok"

[master f73e798] conflict ok
[email protected]:~/joe/learngit$ 
git log --graph --pretty=oneline --abbrev-commit
//檢視分支歷史,形象的描寫了衝突的位置
*   f73e798 conflict ok
|\  
| * 5aa28d8 new bran
* | fe42f3e not a branch
|/  
* 9409b92 new dev branch
* f10fe58 new abc
* 0ad1dfe del ab.c
* 7b6507e new ab.c
* 020f927 del abc
* 010726f del a line
* c834e17 nothing
* aa6b706 new abc.c
* 82f4ed9 modify read
* e32e92b del abc.c
* ab22d92 test stage
* d4e3943 understand how stage workd
* 71038bf append GPL
* 942f575 add distributed
* b401faf joe's first txt
[email protected]:~/joe/learngit$ 
git branch -
d bran
已刪除分支 bran(曾為 5aa28d8)。

分支管理策略

合併分支時,Git會用Fast forward模式,但這種模式下,刪除分支後,會丟掉分支資訊。

如果要強制禁用Fast forward模式,Git就會在merge時生成一個新的commit,這樣,從分支歷史上就可以看出分支資訊。

[email protected]:~/joe/learngit$ 
git checkout -b dev
    //新建dev分支,修改檔案並提交
切換到一個新分支 'dev'
[email protected]:~/joe/learngit$ vi abc.c 
[email protected]:~/joe/learngit$ cat abc.c 
I like you
!

[email protected]:~/joe/learngit$ git add abc.c 
[email protected]:~/joe/learngit$ 
git commit -m "dev branch"

[dev cef4924] dev branch
 1 file changed, 1 insertion(+), 1 deletion(-)
[email protected]:~/joe/learngit$ git checkout master
切換到分支 'master'
您的分支領先 'origin/master'4 個提交。
  (使用 "git push" 來發布您的本地提交)
//合併dev分支,--no-ff引數,表示禁用Fast forward,本次合併要建立一個新的commit,所以加上-m引數,把commit描述寫進去
[email protected]:~/joe/learngit$ 
git merge --no-ff -m "merge with no-ff"
 dev
Merge made by the 'recursive' strategy.
 abc.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
[email protected]:~/joe/learngit$ 
git log --graph --pretty=oneline --abbrev-
commit
*   7877628 merge with no-ff
|\  
| * cef4924 dev branch
|/  
*   f73e798 conflict ok
|\  
| * 5aa28d8 new bran
* | fe42f3e not a branch
|/  
* 9409b92 new dev branch
* f10fe58 new abc

使用fast forward的情況

[email protected]:~/joe/learngit$ 
git checkout -
b dev
切換到一個新分支 'dev'
[email protected]:~/joe/learngit$ vi abc.c 
[email protected]:~/joe/learngit$ cat abc.c 
I don
't like you!
[email protected]:~/joe/learngit$ git add abc.c 
[email protected]:~/joe/learngit$ 
git commit -m "dev"

[dev b961f85] dev
 1 file changed, 1 insertion(+), 1 deletion(-)
[email protected]:~/joe/learngit$ git checkout master
切換到分支 'master'
您的分支領先 'origin/master'6 個提交。
  (使用 "git push" 來發布您的本地提交)
[email protected]:~/joe/learngit$ git merge dev
更新 7877628..b961f85
Fast-forward
 abc.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
[email protected]:~/joe/learngit$ 
git log --graph --pretty=oneline --abbrev-
commit
*
 b961f85 dev
*   7877628 merge with no-ff
|\      //注意這2次分支的對比
| * cef4924 dev branch
|/  
*   f73e798 conflict ok
|\  
| * 5aa28d8 new bran
* | fe42f3e not a branch
|/  
* 9409b92 new dev branch
* f10fe58 new abc
* 0ad1dfe del ab.c

分支策略

  1. 首先,master分支應該是非常穩定的,也就是僅用來發布新版本,平時不能在上面幹活;
  2. 幹活都在dev分支上,dev分支是不穩定的,新版本版本釋出時,再把dev分支合併到master上,在master分支釋出新版本;
  3. 每個人都在dev分支上幹活,每個人都有自己的分支,時不時地往dev分支上合併就可以了。

相關推薦

Git 分支管理策略(5)

分支管理策略   Git通常在合併分支時採用的是“Fast forward” 在這種模式下刪除分支後會丟掉分支資訊,如何進行此模式,使用帶引數--no-ff 禁用此模式。 1.建立一個dev分支,修改readme.txt內容,新增到暫存區, 2.切回主分支master

[git]分支管理策略

一般企業中開發一個專案的分支策略: 主分支 master 開發分支 develop 功能分支 feature 預釋出分支 release bug 分支 fixbug 其它分支 other 1).主分支 master 程式碼庫應該有一個、且僅有一個主分支。所有提供給使用者使用

git--分支管理策略

一、分支管理策略 通常,合併分支時,如果可能,git會用fast forward模式,但是有些快速合併不能成而且合併時沒有衝突,這個時候會合並之後並做一次新的提交。 比如這樣的場景 第一步:建立一個新分支,在新分支中建立一個檔案,在檔案中寫入內容,然後提交 第二步:

Git分支管理策略

如果你嚴肅對待程式設計,就必定會使用"版本管理系統"(Version Control System)。 眼下最流行的"版本管理系統",非Git莫屬。 相比同類軟體,Git有很多優點。其中很顯著的一點,就是版本的分支(branch)和合並(merge)十分方便。有些傳統的版

Git 企業中常用分支管理策略

git Git 企業中常用分支管理策略 Git 分支 Git Git 企業中常用分支管理策略 一般企業中開發一個項目的分支策略 主分支 master 開發分支 develop 功能分支 feature 預發布分支&#160; release bug 分支 fixbug 其它分支 ot

Git安裝教程(三)分支管理分支管理策略

ive comm 歷史 -a adg txt文件 post graph 刪除 通常,合並分支時,如果可能,Git會用Fast forward模式,但這種模式下,刪除分支後,會丟掉分支信息。 如果要強制禁用Fast forward模式,Git就會在merge時生成一個新的co

Git----分支管理分支管理策略04

font image span 切換 ade chan sim 分支策略 ice   通常,合並分支時,如果可能,Git會用Fast forward模式,但這種模式下,刪除分支後,會丟掉分支信息。 如果要強制禁用Fast forward模式,Git就會在merge時生產一個

GIT)程式碼分支管理策略

一、我們採用的管理策略(分支開發主幹釋出)   1. 主分支(master),用於釋出,每次釋出時打一個(tag),不做任何開發使用 拉取源:無 合併目標:無 修改:不允許 生命週期:持續   2. 開發分支(develo

分支管理~策略git merge 合併禁用ff模式 (十一)

通常,合併分支時,如果可能,Git會用Fast forward模式,但這種 ff 模式下,刪除分支後,會丟掉分支資訊。 如果要強制禁用 Fast forward 模式,Git就會在merge時生成一個新的commit,這樣,從分支歷史上就可以看出分支資訊。 下面開始實踐:git merge

Git 學習筆記 - 13 - 分支管理策略

Git 學習筆記 - 13 - 分支管理策略 注:本文參照的是廖雪峰老師的Git教程 概述: Git會用Fast forward模式,但這種模式下,刪除分支後,會丟掉分支資訊。之前的分支在合併後,分支的資訊就沒有了。 什麼叫資訊沒有了,就是之前你在這個分支上的修改版本都不見了

Git解決分支衝突及分支管理策略

        通常當Git無法自動合併分支時,就必須首先解決衝突後,再提交。 下面咱們先建立一個分支並切換到b1分支: 修改咱們之前的hellogit.txt內容,新增一行:Create a new named f1 branch  檢視該檔案的狀態,並提交至本地倉庫: 然後切換至master分支:

git分支管理策略

分支在實際中的作用 假設你準備開發一個新功能,但是需要兩週才能完成,第一週你寫了50%的程式碼,如果立刻提交,由於程式碼還沒寫完,不完整的程式碼庫會導致別人不能幹活了。如果等程式碼全部寫完再一次提交,又存在丟失每天進度的巨大風險。現在有了分支,就不用怕了。你建立了一個屬

Git 分支管理 分支管理策略 不使用Fast forward模式進行合並

強制 ring code string 修改 alt log 就是 圖片         通常,合並分支時,如果可能,Git會用Fast forward模式,但這種模式下,刪除分支後,會丟掉分支信息。   如果要強制禁用Fast forward模式,Git就會在mer

git分支管理策略和衝突問題

[toc] ## 備註: 本文參考於廖雪峰老師的部落格[Git教程](https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000)。依照其部落格進行學習和記錄,感謝其無私分享,也歡迎各位檢視原文。

git 分支管理方案

繼續 終端 class tlab http check toc 團隊 代碼上線 現有一般的公司項目均使用git(大多數是gitLab)管理。 開發組 我們的項目都要建立在 開發組的名下 (git.xxcompany.com/xxgroup),除需要公司內部開源的項目,都必須

git分支管理

issue 就會 查看 無法刪除 date 參與 無法 track git 查看本地分支:$ git branch 查看遠程分支:$ git branch -r 創建本地分支:$ git branch [name] ----註意新分支創建後不會自動切換為當前分支 切換

Git 分支管理

gitGit 分支管理幾乎每一種版本控制系統都以某種形式支持分支。使用分支意味著你可以從開發主線上分離開來,然後在不影響主線的同時繼續工作。有人把 Git 的分支模型稱為"必殺技特性",而正是因為它,將 Git 從版本控制系統家族裏區分出來。創建分支命令:git branch (branc

git分支管理小結

用處 推送 開始 之前 checkout 完成 分支合並 默認 pos 之前在使用git的時候,大部分的用處都是用來將自己本地的代碼上傳至github上。但是近期在做點小項目的時候,發現用到分支的地方,還是挺多的。 這是今天自己看了《github入門與實踐》那本書,跟著書上

Git 分支管理及結合gitlab的使用

git git分支 git分支創建 git分支合並 git分支刪除 Git 分支管理及結合gitlab的使用 說明有關gitlab的說明及基本操作,請參考:http://blog.51cto.com/wutengfei/2090253使用git分支的作用,我們先來說一個簡單的案例吧,你們

4、git分支管理

上推 pac opp 關聯 commit 除了 快速 指定 HR 一、分支的創建與合並   在版本回退裏,你已經知道,每次提交,Git都把它們串成一條時間線,這條時間線就是一個分支。截止到目前,只有一條時間線,在Git裏,這個分支叫主分支,即master分支。HEAD嚴格來