目录
创建分支
git branch [分支]指令
创建新的分⽀后,Git 新建了⼀个指针叫dev, * 表⽰当前 HEAD 指向的分⽀是 master 分⽀。另外,可以通过⽬录结构发现,新的 dev 分⽀:
[paper@VM-16-16-centos ~]$ git branch dev
[paper@VM-16-16-centos ~]$ git branch
dev
* master
切换分支
git checkout [分支]指令
[paper@VM-16-16-centos ~]$ git branch
dev
* master
[paper@VM-16-16-centos ~]$ git checkout dev
Switched to branch 'dev'
[paper@VM-16-16-centos ~]$ git branch
* dev
master
合并分支
master和dev指向的不一样
[paper@VM-16-16-centos ~]$ git checkout dev
Switched to branch 'dev'
[paper@VM-16-16-centos ~]$ vim file
[paper@VM-16-16-centos ~]$ git add file
[paper@VM-16-16-centos ~]$ git commit -m "file"
[dev 04534e7] file
1 file changed, 1 insertion(+)
[paper@VM-16-16-centos ~]$ cat file
hello world
hello git
[paper@VM-16-16-centos ~]$ git checkout master
Switched to branch 'master'
[paper@VM-16-16-centos ~]$ cat file
hello world
此时需要切换至mater分支,使用git merge [分支]指令,合并dev和master分支
[paper@VM-16-16-centos ~]$ cat file
hello world
[paper@VM-16-16-centos ~]$ git merge dev
Updating da0ae2d..04534e7
Fast-forward
file | 1 +
1 file changed, 1 insertion(+)
[paper@VM-16-16-centos ~]$ cat file
hello world
hello git
删除分支
合并完成后, dev 分⽀对于我们来说就没⽤了, 那么dev分⽀就可以被删除掉,注意如果当前正处于某分⽀下,就不能删除当前分⽀
[paper@VM-16-16-centos ~]$ git branch
dev
* master
[paper@VM-16-16-centos ~]$ git branch -d dev
Deleted branch dev (was 04534e7).
[paper@VM-16-16-centos ~]$ git branch
* master
合并冲突
在实际分⽀合并的时候,并不是想合并就能合并成功的,有时候可能会遇到代码冲突的问题。为了演⽰这问题,创建⼀个新的分⽀ dev1 ,并切换⾄⽬标分⽀,我们可以使⽤ git checkout - b dev1 ⼀步完成创建并切换的动作,⽰例如下:
[paper@VM-16-16-centos ~]$ git checkout master
D file1
Switched to branch 'master'
[paper@VM-16-16-centos ~]$ cat file
hello git
[paper@VM-16-16-centos ~]$ git branch
dev
* master
[paper@VM-16-16-centos ~]$ git checkout dev
D file1
Switched to branch 'dev'
[paper@VM-16-16-centos ~]$ git branch
* dev
master
[paper@VM-16-16-centos ~]$ cat file
hello world
发现file⽂件有冲突后,可以直接查看⽂件内容,要说的是 Git 会⽤ <<<<<<<,=======,>>>>>>> 来标记出不同分⽀的冲突内容,如下所⽰:
[paper@VM-16-16-centos ~]$ git merge dev
Auto-merging file
CONFLICT (add/add): Merge conflict in file
Automatic merge failed; fix conflicts and then commit the result.
[paper@VM-16-16-centos ~]$ cat file
<<<<<<< HEAD
hello git
=======
hello world
>>>>>>> dev
此时我们必须要⼿动调整冲突代码,并需要再次提交修正后的结果!!
[paper@VM-16-16-centos ~]$ vim file
[paper@VM-16-16-centos ~]$ git add file
[paper@VM-16-16-centos ~]$ git commit -m "file"
[master c3de9f5] file
[paper@VM-16-16-centos ~]$ cat file
hello world