git有一個默認的主分支(master),一般master分支的版本應該是很是穩定的,即用來發布版本使用的,通常狀況下不容許直接在上面修改。若是咱們要修復一些bug,或是增長一些新功能,通常都會新建一個dev(表示開發分支)分支,在開發分支上完成開發功能,測試經過以後,再將dev分支上的修改合併到master上面來。
#1.查看本地全部分支git
sand@sand_pc MINGW64 /h/gitRepositories/firstRepo (master) $ git branch * master
由上可知:firstRepo倉庫只有一個master分支。
#2.新建分支測試
sand@sand_pc MINGW64 /h/gitRepositories/firstRepo (master) $ git branch * master sand@sand_pc MINGW64 /h/gitRepositories/firstRepo (master) $ git branch dev sand@sand_pc MINGW64 /h/gitRepositories/firstRepo (master) $ git branch dev * master sand@sand_pc MINGW64 /h/gitRepositories/firstRepo (master) $
*表示目前正處於哪一個分支上。
#3.切換分支3d
sand@sand_pc MINGW64 /h/gitRepositories/firstRepo (master) $ git checkout dev Switched to branch 'dev' sand@sand_pc MINGW64 /h/gitRepositories/firstRepo (dev) $ git branch * dev master sand@sand_pc MINGW64 /h/gitRepositories/firstRepo (dev) $
目前切換到了dev分支。
查看dev分支下文件code
sand@sand_pc MINGW64 /h/gitRepositories/firstRepo (dev) $ ls test.txt
#4.合併分支
首先,咱們在dev分支上作開發,在test.txt中再添加內容,查看內容以下開發
sand@sand_pc MINGW64 /h/gitRepositories/firstRepo (dev) $ cat test.txt 123456 789012 abcdef sand@sand_pc MINGW64 /h/gitRepositories/firstRepo (dev) $
如上,abcdef是新增的。
而後測試經過,提交到dev分支上,以下it
sand@sand_pc MINGW64 /h/gitRepositories/firstRepo (dev) $ git add test.txt sand@sand_pc MINGW64 /h/gitRepositories/firstRepo (dev) $ git commit -m "dev分支上新增內容:abcdef" [dev b0c5e3e] dev分支上新增內容:abcdef 1 file changed, 2 insertions(+), 1 deletion(-) sand@sand_pc MINGW64 /h/gitRepositories/firstRepo (dev) $
如今dev分支的開發工做完成了,切換到master上來,以下io
sand@sand_pc MINGW64 /h/gitRepositories/firstRepo (dev) $ git checkout master Switched to branch 'master' sand@sand_pc MINGW64 /h/gitRepositories/firstRepo (master) $
查看master分支上的test.txt內容,以下ast
sand@sand_pc MINGW64 /h/gitRepositories/firstRepo (master) $ cat test.txt 123456 789012 sand@sand_pc MINGW64 /h/gitRepositories/firstRepo (master) $
很明顯,master分支上的test.txt中並無內容:abcdef。
如今須要將dev分支上開發的內容合併到master分支上,採用以下命令test
sand@sand_pc MINGW64 /h/gitRepositories/firstRepo (master) $ git merge dev Updating da94b84..b0c5e3e Fast-forward test.txt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) sand@sand_pc MINGW64 /h/gitRepositories/firstRepo (master) $ cat test.txt 123456 789012 abcdef sand@sand_pc MINGW64 /h/gitRepositories/firstRepo (master) $
如上,master分支上有了內容abcdef。
#5.刪除分支
上述合併成功了,所以dev分支沒有什麼做用了,這裏刪除dev分支,命令以下file
sand@sand_pc MINGW64 /h/gitRepositories/firstRepo (master) $ git branch -d dev Deleted branch dev (was b0c5e3e). sand@sand_pc MINGW64 /h/gitRepositories/firstRepo (master) $ git branch * master
#6.處理衝突
上述的合併很成功,沒有任何衝突。可是在實際開發中,尤爲是團隊多人開發的時候,多我的可能會操做同一個文件,所以,合併的時候可能會存在衝突,以下就衝突舉例。
##6.1 新建測試分支test
sand@sand_pc MINGW64 /h/gitRepositories/firstRepo (master) $ git branch test sand@sand_pc MINGW64 /h/gitRepositories/firstRepo (master) $ git checkout test Switched to branch 'test' sand@sand_pc MINGW64 /h/gitRepositories/firstRepo (test) $
修改test.txt中內容,修改先後內容以下
sand@sand_pc MINGW64 /h/gitRepositories/firstRepo (test) $ cat test.txt 123456 789012 abcdef sand@sand_pc MINGW64 /h/gitRepositories/firstRepo (test) $ cat test.txt 123456 789012 abcdef uvwxyz sand@sand_pc MINGW64 /h/gitRepositories/firstRepo (test) $
很明顯,增長了內容:uvwxyz。
接着提交文件
sand@sand_pc MINGW64 /h/gitRepositories/firstRepo (test) $ git add test.txt sand@sand_pc MINGW64 /h/gitRepositories/firstRepo (test) $ git commit -m "新增內容:uvwxyz" [test fcda1ba] 新增內容:uvwxyz 1 file changed, 2 insertions(+), 1 deletion(-) sand@sand_pc MINGW64 /h/gitRepositories/firstRepo (test) $
接着切換到master分支上
sand@sand_pc MINGW64 /h/gitRepositories/firstRepo (test) $ git checkout master D a.txt Switched to branch 'master' sand@sand_pc MINGW64 /h/gitRepositories/firstRepo (master) $ cat test.txt 123456 789012 abcdef sand@sand_pc MINGW64 /h/gitRepositories/firstRepo (master) $
很明顯,master分支上的內容是沒有uvwxyz的,如今咱們也來修改master上的test.txt文件
sand@sand_pc MINGW64 /h/gitRepositories/firstRepo (master) $ cat test.txt 123456 789012 abcdef sand@sand_pc MINGW64 /h/gitRepositories/firstRepo (master) $ cat test.txt 123456 789012 abcdef 111111 sand@sand_pc MINGW64 /h/gitRepositories/firstRepo (master) $
提交文件
sand@sand_pc MINGW64 /h/gitRepositories/firstRepo (master) $ git add test.txt sand@sand_pc MINGW64 /h/gitRepositories/firstRepo (master) $ git commit -m "新增內容:111111" [master 93dfaa5] 新增內容:111111 1 file changed, 2 insertions(+), 1 deletion(-) sand@sand_pc MINGW64 /h/gitRepositories/firstRepo (master) $
接着咱們將test分支合併到master分支上
sand@sand_pc MINGW64 /h/gitRepositories/firstRepo (master) $ git merge test Auto-merging test.txt CONFLICT (content): Merge conflict in test.txt Automatic merge failed; fix conflicts and then commit the result. sand@sand_pc MINGW64 /h/gitRepositories/firstRepo (master|MERGING) $
很明顯,出現衝突了。咱們查看衝突文件
sand@sand_pc MINGW64 /h/gitRepositories/firstRepo (master|MERGING) $ cat test.txt 123456 789012 abcdef <<<<<<< HEAD 111111 ======= uvwxyz >>>>>>> test sand@sand_pc MINGW64 /h/gitRepositories/firstRepo (master|MERGING) $
git使用<<<<<<<,======,>>>>>>來標記不一樣分支的內容,其中
<<<<<<HEAD表示主分支修改的內容
">>>>>>"test表示test分支修改的內容
咱們須要手動的解決衝突,而後提交就行
sand@sand_pc MINGW64 /h/gitRepositories/firstRepo (master|MERGING) $ cat test.txt 123456 789012 abcdef <<<<<<< HEAD 111111 ======= uvwxyz >>>>>>> test sand@sand_pc MINGW64 /h/gitRepositories/firstRepo (master|MERGING) $ cat test.txt 123456 789012 abcdef 111111 uvwxyz sand@sand_pc MINGW64 /h/gitRepositories/firstRepo (master|MERGING) $
而後提交
sand@sand_pc MINGW64 /h/gitRepositories/firstRepo (master|MERGING) $ git add test.txt sand@sand_pc MINGW64 /h/gitRepositories/firstRepo (master|MERGING) $ git commit -m "解決衝突" [master 99ec744] 解決衝突 sand@sand_pc MINGW64 /h/gitRepositories/firstRepo (master) $