1、一开始,只有一个主分支(master),HEAD指向Master,而Master指向主分支。现在我们创建dev分支。
lfy@lfy-PC MINGW64 /c/fyliu/lfyTemp/gitLocalRepository/yangjie (master) $ git checkout -b dev Switched to a new branch 'dev' lfy@lfy-PC MINGW64 /c/fyliu/lfyTemp/gitLocalRepository/yangjie (dev) $
git checkout
命令加上-b
参数表示创建并切换,相当于以下两条命令:
$ git branch dev $ git checkout dev Switched to branch 'dev'
然后,用git branch
命令查看当前分支:
lfy@lfy-PC MINGW64 /c/fyliu/lfyTemp/gitLocalRepository/yangjie (dev) $ git branch * dev master lfy@lfy-PC MINGW64 /c/fyliu/lfyTemp/gitLocalRepository/yangjie (dev) $
git branch
命令会列出所有分支,当前分支前面会标一个*
号。然后,我们就可以在dev
分支上正常提交,比如对readme.txt
做个修改。
lfy@lfy-PC MINGW64 /c/fyliu/lfyTemp/gitLocalRepository/yangjie (dev) $ vim readme.txt lfy@lfy-PC MINGW64 /c/fyliu/lfyTemp/gitLocalRepository/yangjie (dev) $ git add readme.txt lfy@lfy-PC MINGW64 /c/fyliu/lfyTemp/gitLocalRepository/yangjie (dev) $ git commit -m "branch test" [dev 1df3a8d] branch test 1 file changed, 1 insertion(+) create mode 100644 readme.txt
切换到master分支。
lfy@lfy-PC MINGW64 /c/fyliu/lfyTemp/gitLocalRepository/yangjie (dev) $ git checkout master Switched to branch 'master' Your branch is up to date with 'origin/master'. lfy@lfy-PC MINGW64 /c/fyliu/lfyTemp/gitLocalRepository/yangjie (master) $ git branch dev * master lfy@lfy-PC MINGW64 /c/fyliu/lfyTemp/gitLocalRepository/yangjie (master) $ ls LICENSE README.md lfy@lfy-PC MINGW64 /c/fyliu/lfyTemp/gitLocalRepository/yangjie (master) $
刚才在dev分支的readme.txt文件不见了。将dev分支修改的内容合并到master分支:
命令:git merge branch_name
解析:用于合并指定分支到当前分支。合并后,再查看readme.txt
的内容,就可以看到,和dev
分支的最新提交是完全一样的。
lfy@lfy-PC MINGW64 /c/fyliu/lfyTemp/gitLocalRepository/yangjie (dev) $ git checkout master Switched to branch 'master' Your branch is up to date with 'origin/master'. lfy@lfy-PC MINGW64 /c/fyliu/lfyTemp/gitLocalRepository/yangjie (master) $ git branch dev * master lfy@lfy-PC MINGW64 /c/fyliu/lfyTemp/gitLocalRepository/yangjie (master) $ git merge dev Updating d1b1bf0..1df3a8d Fast-forward readme.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 readme.txt lfy@lfy-PC MINGW64 /c/fyliu/lfyTemp/gitLocalRepository/yangjie (master) $
Fast-forward
信息,Git告诉我们,这次合并是“快进模式”,也就是直接把master
指向dev
的当前提交,所以合并速度非常快。当然,也不是每次合并都能Fast-forward
,我们后面会讲其他方式的合并。
合并完成后,就可以放心地删除dev
分支了:
lfy@lfy-PC MINGW64 /c/fyliu/lfyTemp/gitLocalRepository/yangjie (master) $ git branch -d dev Deleted branch dev (was 1df3a8d). lfy@lfy-PC MINGW64 /c/fyliu/lfyTemp/gitLocalRepository/yangjie (master) $ git branch * master
2、解决冲突