git之分支
创建分⽀
Git ⽀持我们查看或创建其他分⽀,在这⾥我们来创建第⼀个⾃⼰的分⽀ dev ,对应的命令为:
1 hyb@139-159-150-152:~/gitcode$ git branch #查看当前本地所有分⽀
2 * master
3 hyb@139-159-150-152:~/gitcode$ git branch dev #新建分⽀dev
4 hyb@139-159-150-152:~/gitcode$ git branch
5 dev
6 * master
当我们创建新的分⽀后,Git 新建了⼀个指针叫 dev, * 表⽰当前 HEAD 指向的分⽀是 master 分⽀。另外,可以通过⽬录结构发现,新的 dev 分⽀:
1 hyb@139-159-150-152:~/gitcode$ ls .git/refs/heads/
2 dev master
3 hyb@139-159-150-152:~/gitcode$ cat .git/refs/heads/*
4 5476bdeb12510f7cd72ac4766db7988925ebd302
5 5476bdeb12510f7cd72ac4766db7988925ebd302
发现⽬前 dev 和 master 指向同⼀个修改。并且也可以验证下 HEAD ⽬前是指向 master 的。
1 hyb@139-159-150-152:~/gitcode$ cat .git/HEAD
2 ref: refs/heads/master
⼀张图总结:
切换分⽀
那如何切换到 dev 分⽀下进⾏开发呢?使⽤ git checkout 命令即可完成切换,⽰例如下:
1 hyb@139-159-150-152:~/gitcode$ git checkout dev
2 Switched to branch 'dev'
3 hyb@139-159-150-152:~/gitcode$ git branch
4 * dev
5 master
6 hyb@139-159-150-152:~/gitcode$ cat .git/HEAD
7 ref: refs/heads/dev
我们发现 HEAD 已经指向了 dev,就表⽰我们已经成功的切换到了 dev 上!
接下来,在 dev 分⽀下修改 ReadMe ⽂件,新增⼀⾏内容,并进⾏⼀次提交操作:
1 hyb@139-159-150-152:~/gitcode$ vim ReadMe
2 hyb@139-159-150-152:~/gitcode$ cat ReadMe
3 hello bit
4 hello git
5 hello world
6 hello version1
7 hello version2
8 hello version3
9 write aaa for new branch
10 hyb@139-159-150-152:~/gitcode$ git add .
11 hyb@139-159-150-152:~/gitcode$ git commit -m"modify ReadMe"
12 [dev 3740dce] modify ReadMe
13 1 file changed, 1 insertion(+)
现在,dev 分⽀的⼯作完成,我们就可以切换回 master 分⽀:
1 hyb@139-159-150-152:~/gitcode$ git checkout master
2 Switched to branch 'master'
3 hyb@139-159-150-152:~/gitcode$ cat ReadMe
4 hello bit
5 hello git
6 hello world
7 hello version1
8 hello version2
9 hello version3
切换回 master 分⽀后,发现ReadMe⽂件中新增的内容不⻅了!!!赶紧再切回 dev 看看:
1 hyb@139-159-150-152:~/gitcode$ git checkout dev
2 Switched to branch 'dev'
3 hyb@139-159-150-152:~/gitcode$ cat ReadMe
4 hello bit
5 hello git
6 hello world
7 hello version1
8 hello version2
9 hello version3
10 write aaa for new branch
在 dev 分⽀上,内容还在。为什么会出现这个现象呢?我们来看看 dev 分⽀和 master 分⽀指向,发现两者指向的提交是不⼀样的:
1 hyb@139-159-150-152:~/gitcode$ cat .git/refs/heads/dev
2 bdaf528ffbb8e05aee34d37685408f0e315e31a4
3 hyb@139-159-150-152:~/gitcode$ cat .git/refs/heads/master
4 5476bdeb12510f7cd72ac4766db7988925ebd302
当切换到 master 分⽀之时,HEAD 就指向了 master,当然看不到提交了!
合并分⽀
为了在 master 主分⽀上能看到新的提交,就需要将 dev 分⽀合并到 master 分⽀,⽰例如下:
1 hyb@139-159-150-152:~/gitcode$ git branch
2 * dev
3 master
4 hyb@139-159-150-152:~/gitcode$ git checkout master # 切换到 master 上进⾏合并
5 Switched to branch 'master'
6 hyb@139-159-150-152:~/gitcode$ git merge dev # 合并 dev 分⽀
7 Updating 16623e1..3740dce
8 Fast-forward
9 ReadMe | 1 +
10 1 file changed, 1 insertion(+)
11 hyb@139-159-150-152:~/gitcode$ cat ReadMe
12 hello bit
13 hello git
14 hello world
15 hello version1
16 hello version2
17 hello version3
18 write aaa for new branch
git merge 命令⽤于合并指定分⽀到当前分⽀。合并后,master 就能看到 dev 分⽀提交的内容了。此时的状态如图如下所⽰。
Fast-forward 代表“快进模式”,也就是直接把master指向dev的当前提交,所以合并速度⾮常快。当然,也不是每次合并都能 Fast-forward,我们后⾯会讲其他⽅式的合并。
删除分⽀
合并完成后, dev 分⽀对于我们来说就没⽤了, 那么dev分⽀就可以被删除掉,注意如果当前正处于某分⽀下,就不能删除当前分⽀,如:
1 hyb@139-159-150-152:~/gitcode$ git branch
2 * dev
3 master
4 hyb@139-159-150-152:~/gitcode$ git branch -d dev
5 error: Cannot delete branch 'dev' checked out at '/home/hyb/gitcode'
⽽可以在其他分⽀下删除当前分⽀,如:
1 hyb@139-159-150-152:~/gitcode$ git checkout master
2 Switched to branch 'master'
3 hyb@139-159-150-152:~/gitcode$ git branch -d dev
4 Deleted branch dev (was bdaf528).
5 hyb@139-159-150-152:~/gitcode$ git branch
6 * master
7
此时的状态如图如下所⽰。