简介
在开发大型项目中,很多情况下不同开发人员负责开发不同的模块,这时效率就显得尤为重要,git的分支管理明确而简洁地解决了大部分问题
用法
-
新建一个分支
[lkc@lkcdeMacBook-Pro:] ~/Desktop/Code/gitSample $ git br * master [lkc@lkcdeMacBook-Pro:] ~/Desktop/Code/gitSample $ git checkout -b master-a Switched to a new branch 'master-a' [lkc@lkcdeMacBook-Pro:] ~/Desktop/Code/gitSample $ git br master * master-a [lkc@lkcdeMacBook-Pro:] ~/Desktop/Code/gitSample $
如图所示,本地仓库默认有一个master分支,这时我们如果新建一个分支master-a,此时跟master是指向一个版本的,然后我们转移到master-a分支上完成我们的工作即可;
那么,会产生一个问题,git如何知道我们现在处于哪个分支呢;如图,git有一个HEAD指针用来指向本地当前所在的分支,所以在我们转移到master-a分支后,HEAD也指向了master-a分支. -
合并分支
[lkc@lkcdeMacBook-Pro:] ~/Desktop/Code/gitSample $ git br master * master-a [lkc@lkcdeMacBook-Pro:] ~/Desktop/Code/gitSample $ vi a.txt [lkc@lkcdeMacBook-Pro:] ~/Desktop/Code/gitSample $ git add a.txt [lkc@lkcdeMacBook-Pro:] ~/Desktop/Code/gitSample $ git commit -m "master-a" [master-a 9db9161] master-a 1 file changed, 1 insertion(+), 1 deletion(-) [lkc@lkcdeMacBook-Pro:] ~/Desktop/Code/gitSample $ git co master Switched to branch 'master' [lkc@lkcdeMacBook-Pro:] ~/Desktop/Code/gitSample $ vi a.txt [lkc@lkcdeMacBook-Pro:] ~/Desktop/Code/gitSample $ git add a.txt [lkc@lkcdeMacBook-Pro:] ~/Desktop/Code/gitSample $ git commit -m "master" [master 4c88be5] master 1 file changed, 1 insertion(+), 1 deletion(-) [lkc@lkcdeMacBook-Pro:] ~/Desktop/Code/gitSample $ git merge master-a Auto-merging a.txt CONFLICT (content): Merge conflict in a.txt Automatic merge failed; fix conflicts and then commit the result.
我们在master-a分支上做了一些工作之后想把工作合并到原分支上,这时会产生一些冲突,因为有可能原来的master分支也被修改过了,所以我们要解决冲突;
解决的办法就是找到产生冲突的文件,里面会显示master与master-a冲突的行,<<<所在的位置就两个分支冲突的地方,上下通过====分割1 <<<<<<< HEAD 2 asdassss 3 ======= 4 asdadassss 5 >>>>>>> master-a 6 bbbb 7 adasdaaaaa 8 assssasdaddss 9 asdad 10 ss
修改好后再次合并就可以了.
[lkc@lkcdeMacBook-Pro:] ~/Desktop/Code/gitSample $ git merge master-a error: Merging is not possible because you have unmerged files. hint: Fix them up in the work tree, and then use 'git add/rm <file>' hint: as appropriate to mark resolution and make a commit. fatal: Exiting because of an unresolved conflict. [lkc@lkcdeMacBook-Pro:] ~/Desktop/Code/gitSample $ git add a.txt [lkc@lkcdeMacBook-Pro:] ~/Desktop/Code/gitSample $ git commit -m "sure" [master 7a14172] sure [lkc@lkcdeMacBook-Pro:] ~/Desktop/Code/gitSample $ git merge master-a Already up-to-date.
常用命令
命令 | 作用 |
---|---|
git branch | 显示所有分支 |
git branch a | 新建一个名为a的分支 |
git checkout -b a | 新建一个名为a的分支并切换上去 |
git branch -d a | 删除名为a的分支 |
git merge a | 将当前分支与a合并 |
git checkout a | 切换到a分支上 |