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