几乎全部的版本控制都以某种形式支持分支。使用分支意味着你能够把你的工做从开发主线上分离开来,以避免影响开发主线。git
Git的分支模型成称为它的“必杀技特性”,也正由于这一特性,使得Git从众多版本控制系统中脱颖而出。Git处理分支的方式是难以置信的轻量,建立新的分支这一操做是秒级完成的,而且在不一样分支之间的切换操做也是同样便捷。vim
Git的分支,其实本质上仅仅是指向提交对象的可变指针。Git的默认分支是master。在屡次提交操做以后,其实咱们已经有一个指向最后那个提交对象的master分支。他会在每次的提交操做中自动向前移动。app
实际工做中咱们可能会遇到一个下面这个状况:ide
- 开发某个网站。
- 为实现某个新的需求,建立一个分支。
- 在这个分支上开展工做。
- 正在此时,你忽然接到一个电话说有个很严重的问题须要紧急修补。 你将按照以下方式来处理:
- 切换到你的线上分支(production branch)。
- 为这个紧急任务新建一个分支,并在其中修复它。
- 在测试经过以后,切换回线上分支,而后合并这个修补分支,最后将改动推送到线上分支。
- 修改后切换回你最初工做的分支上,继续工做。
一、初始化一个目录并声明用户及邮箱地址测试
[root@git /]# mkdir /git [root@git /]# cd git/ [root@git git]# git init Initialized empty Git repository in /git/.git/ [root@git git]# ls -a . .. .git [root@git git]# git config --global user.name admin [root@git git]# git config --global user.email admin@admin.com
二、建立、快速合并及删除分支网站
[root@git git]# echo "aaaa" > branch.txt [root@git git]# git add branch.txt [root@git git]# git commit -m "第一次提交From master" [master (root-commit) b9f1465] 第一次提交From master 1 file changed, 1 insertion(+) create mode 100644 branch.txt #建立一个分支并进入新建的分支 [root@git git]# git checkout -b dev Switched to a new branch 'dev' [root@git git]# git branch #查看当前所在分支 * dev # 星号所在的列就是当前所在分支 master #在dev分支更新文件并提交 [root@git git]# echo "bbbb" >> branch.txt [root@git git]# git add * [root@git git]# git commit -m "commit From dev branch" [dev e9989ba] commit From dev branch 1 file changed, 1 insertion(+) [root@git git]# cat branch.txt # 确认当前内容 aaaa bbbb [root@git git]# git checkout master # 切换到主分支 Switched to branch 'master' [root@git git]# cat branch.txt # 查看内容 aaaa [root@git git]# git merge dev # 合并dev分支 Updating b9f1465..e9989ba Fast-forward branch.txt | 1 + 1 file changed, 1 insertion(+) [root@git git]# cat branch.txt # 再次查看内容 aaaa bbbb [root@git git]# git log --graph --pretty=oneline --abbrev-commit #查看提交日志 * e9989ba commit From dev branch * b9f1465 第一次提交From master #能够查看到其提交日志前面的星号是在同列的,这是由于采用的是快速合并的方法(默认) #稍后会写下关闭快速合并,而后能够对比该命令查看的结果 [root@git git]# git branch -d dev # 删除dev分支 Deleted branch dev (was e9989ba).
三、解决分支冲突问题ui
在咱们实际工做中会遇到一个分支冲突的问题,就是当你在工做分支dev下对文件内容进行了修改,而后在你提交到版本库前,master分支下的内容已经发生了改变,此时,你dev分支下的内容是比master下的内容要旧,这种状况下进行分支合并,会有一个分支冲突的概念,例子以下: [root@git git]# cat branch.txt # 查看当前内容 aaaa bbbb [root@git git]# git checkout -b dev # 建立一个分支并切换至本身的工做分支 Switched to a new branch 'dev' [root@git git]# echo "cccc" >> branch.txt # 写入新数据 [root@git git]# git add branch.txt [root@git git]# git commit -m "alter from dev" [dev 051082c] alter from dev 1 file changed, 1 insertion(+) [root@git git]# cat branch.txt # 查看当前内容 aaaa bbbb cccc [root@git git]# git checkout master # 切换至主分支 Switched to branch 'master' [root@git git]# cat branch.txt # 查看内容仍是以前的 aaaa bbbb #修改master下的文件内容并提交 [root@git git]# echo "dddd" >> branch.txt # 写入新数据 [root@git git]# git add branch.txt [root@git git]# git commit -m "alter from master" [master 1303111] alter from master 1 file changed, 1 insertion(+) [root@git git]# cat branch.txt # 肯定当前内容 aaaa bbbb dddd #接下来将dev分支进行合并: [root@git git]# git merge dev Auto-merging branch.txt CONFLICT (content): Merge conflict in branch.txt # 合并冲突于 branch.txt Automatic merge failed; fix conflicts and then commit the result. #解决合并冲突 #其实有上述报错后,dev分支下的内容已经存在了master目录下的文件中,只是没有提交而已,提交便可 #可是工做中不建议直接提交,由于内容有些特殊的地方 [root@git git]# cat branch.txt # 此时文件的内容以下 aaaa bbbb <<<<<<< HEAD dddd ======= cccc >>>>>>> dev [root@git git]# cat branch.txt #是用vim命令进入修改,将冲突报错产生的特殊符号删除再提交 aaaa bbbb dddd cccc [root@git git]# git add branch.txt [root@git git]# git commit -m "冲突已解决" [master 26ff6b8] 冲突已解决 [root@git git]# git log --graph --pretty=oneline --abbrev-commit #查看分支合并状况 * 26ff6b8 冲突已解决 |\ | * 051082c alter from dev * | 1303111 alter from master |/ * e9989ba commit From dev branch * b9f1465 第一次提交From master
四、关闭快速合并
在上面说到,在查看git版本的提交历史时,其分支结构表现的不是那么直观,那是由于默认开启了快速合并的选项,这里写下如何关闭快速合并3d
#进入分支,修改文件内容,并提交 [root@git git]# git checkout -b dev Switched to a new branch 'dev' [root@git git]# echo "123" >> branch.txt [root@git git]# git add branch.txt [root@git git]# git commit -m "关闭快速合并" [dev 3d40b6a] 关闭快速合并 1 file changed, 1 insertion(+) #切换至master分支,进行合并 [root@git git]# git checkout master Switched to branch 'master' [root@git git]# git merge --no-ff -m "分支合并说明" dev #选项“--no--ff”就是关闭快速合并 Merge made by the 'recursive' strategy. branch.txt | 1 + 1 file changed, 1 insertion(+) [root@git git]# git log --graph --pretty=oneline --abbrev-commit #再次查看提交日志 * f438cbf 分支合并说明 |\ | * 3d40b6a 关闭快速合并 |/ * 26ff6b8 冲突已解决 |\ | * 051082c alter from dev * | 1303111 alter from master |/ #如下是最初没有关闭快速合并的分支合并操做,能够看到只有一列星号,而不显示分支 * e9989ba commit From dev branch * b9f1465 第一次提交From master
五、Bug分支
开发人员在开发过程当中,bug就像屡见不鲜同样,有了bug就要修复,在git中,因为分支是强大的,因此均可以经过一个新的临时分支来修复bug,修复后,分支合并,而后将临时分支删除。版本控制
当咱们接到一个修改bug的任务后,很天然的想要建立一个分支来修复它,可是当前正在进行的工做进行到一半,还没法提交,但又须要立刻修复bug,此时,能够经过git提供的stash功能,能够把当前工做区“储藏”起来,等之后恢复现场后继续工做。
工做进行到一半时工做区的状态以下:指针
[root@git git]# cat branch.txt aaaa bbbb dddd cccc 123 [root@git git]# echo "321321" >> branch.txt [root@git git]# git status #提示修改可是还没有提交 # On branch master # Changes not staged for commit: # (use "git add <file>..." to update what will be committed) # (use "git checkout -- <file>..." to discard changes in working directory) # # modified: branch.txt # no changes added to commit (use "git add" and/or "git commit -a")
将此时的工做区隐藏起来
[root@git git]# git stash # 这条命令,能够将当前工做区隐藏 Saved working directory and index state WIP on master: f438cbf 分支合并说明 HEAD is now at f438cbf 分支合并说明 [root@git git]# git status # 查看状态,如今是干净的 # On branch master nothing to commit, working directory clean [root@git git]# cat branch.txt #文件也没有咱们新加的内容 aaaa bbbb dddd cccc 123
假设在master分支上修复bug
#切换至master分支并进入bug分支修改 [root@git git]# git checkout master [root@git git]# git checkout -b bug Switched to a new branch 'bug' [root@git git]# echo "eeee" >> branch.txt [root@git git]# git add branch.txt [root@git git]# git commit -m "alter from bug" [bug 1cdff4b] alter from bug 1 file changed, 1 insertion(+) #切换至master分支合并修改后的bug分支 [root@git git]# git checkout master Switched to branch 'master' [root@git git]# git merge bug Updating f438cbf..1cdff4b Fast-forward branch.txt | 1 + 1 file changed, 1 insertion(+) [root@git git]# cat branch.txt #合并后的文件内容以下 aaaa bbbb dddd cccc 123 eeee [root@git git]# git branch -d bug # 删除bug分支 Deleted branch bug (was 1cdff4b). #回到dev分支恢复以前修改的内容继续本身的工做 #有两种恢复方法: #一是使用 git stash apply 恢复,可是恢复后,stash 内容并不删除,须要用 git stash drop 来删除; #另外一种方式是用 git stash pop,恢复的同时把 stash 内容也删了;这里我采用第二种方法 [root@git git]# git stash pop #恢复存储区的内容 Auto-merging branch.txt CONFLICT (content): Merge conflict in branch.txt [root@git git]# cat branch.txt #咱们以前的内容又回来了 aaaa bbbb dddd cccc 123 <<<<<<< Updated upstream eeee ======= 321321 >>>>>>> Stashed changes #最后工做完成,在合并dev分支的时候,也会有分支冲突,能够参考前面解决分支冲突的方法
六、Git分支管理相关命令
[root@git ll]# git checkout -b ops #建立ops分支并切换到ops分支 [root@git ll]# git checkout master #切换至master分支 [root@git ll]# git merge dev #快速合并dev分区到当前分支 [root@git ll]# git branch -d ui #删除ui分支 [root@git ll]# git branch #查看所在分支(用星号表示所在分支) [root@git ll]# git log --graph --pretty=oneline --abbrev-commit #查看分支合并图 [root@git ll]# git merge --no-ff -m "合并时提交信息" dev #不使用快速合并分支 [root@git ll]# git stash #将当前版本库的状态临时存储 [root@git ll]# git stash pop #恢复并删除临时存储的信息 [root@git ll]# git stash apply #恢复临时存储信息,但不删除信息 [root@git ll]# git stash drop #删除临时存储中的信息 [root@git ll]# git stash show #查看临时存储的信息 [root@git ll]# git branch -D dev #强制删除一个分支 [root@git ll]# git remote #查看当前版本库是否属于远程版本库 [root@git ll]# git remote -v #查看远程版本库的详细信息 [root@git ll]# git push origin dev #将本地dev分支推送到远程仓库 [root@git ll]# git checkout -b dev origin/dev #建立本地dev分支并关联到远程仓库的dev分支 [root@git ll]# git pull #抓取远程分支,通常用于解决冲突 [root@git ll]# git branch --set-upstream-to=origin/dev dev #将本地分支dev关联到远程仓库的dev分支