分支在实际中有什么用呢?假设你准备开发一个新功能,可是须要两周才能完成,第一周你写了50%的代码,马上提交,因为代码尚未写完,不完整的代码库会致使别人不能干活了。若是等代码所有写完再一次提交,又存在丢失天天进度的风险。
如今有了分支,就不能怕了,你建立了一个属于你本身的分支,别人看不到,还继续在原来的分支上正常工做,而你在本身的分支上干活,想提交就提交,直到开发完毕后,在一次性合并到原来的分支上,这样,既安全,又不影响别人工做。git
Git的分支与其余版本控制系统不一样,不管建立,切换和删除分支,Git在1秒钟以内就能完成!不管你的版本库是1个文件仍是1万个文件。github
在版本回退里,咱们已经知道,每次提交,Git都把它们串成一条时间线,这条时间线就是一个分支。截止到目前,只有一条时间线,在Git里,这个分支叫主分支(master分支),HEAD严格来讲不是指向提交,而是指向master,master才是指向提交的,因此,HEAD指向的就是当前分支。安全
① 一开始的时候,master分支是一条线,Git用master指向最新的提交,再用HEAD执行maser,就能肯定当前分支,以及当前分支的提交点,每次提交,master分支都会向前一步:
② 当咱们建立新的分支,例如dev时,Git建立了一个指针叫dev,指向master相同的提交,再把HEAD指向dev,就表示当前分支在dev上:
能够看到,Git建立一个分支很快,由于除了增长一个dev指针,改改HEAD的指向,工做区的文件都没有任何变化。app
③ 不过,从如今开始,对工做区的修改和提交就是针对dev分支了,好比新提交一次后,dev指针往前移动一步,而master指针不变:分布式
④ 假如咱们在dev上的工做完成了,就能够把dev合并到master上。Git合并很简单,就是把master指向dev的当前提交,就完成了合并:
因此Git合并分支也很快,就改改指针,工做区内容不变。ide
⑤ 合并完分支后,甚至能够删除dev分支。删除dev分支就是把dev指针给删掉,咱们就剩下了一条master分支:fetch
下面开始进行实践:
首先,咱们建立dev分支,而后切换到dev分支:this
[jonson@localhost mygit]$ git checkout -b dev Switched to a new branch 'dev' #git checkout命令加上-b参数表示建立并切换,至关于如下两条命令: $ git branch dev $ git checkout dev
而后,用git branch命令查看当前分支:3d
[jonson@localhost mygit]$ git branch * dev master #git branch命令会列出全部分支,当前分支前面会标一个*符号
而后,咱们就能够在dev分支上正常提交,好比对test.txt文件作修改:版本控制
[jonson@localhost mygit]$ echo "create a new brach" >> text.txt [jonson@localhost mygit]$ cat text.txt # this is jonson first repo create a new brach 而后提交: [jonson@localhost mygit]$ git add text.txt [jonson@localhost mygit]$ git commit -m "branch test" [dev e2d7f2d] branch test 1 file changed, 1 insertion(+)
如今,dev分支的工做完成,咱们就能够切换回master分支:
[jonson@localhost mygit]$ git checkout master Switched to branch 'master' [jonson@localhost mygit]$ git branch dev * master [jonson@localhost mygit]$ cat text.txt # this is jonson first repo
切换回master分支后,在查看刚刚修改的文件,刚才添加的内容不见了。缘由是那个提交是在dev分支上,而master分支此刻的提交点并无变:
合并分支:
如今,咱们把dev分支的工做成果合并到master分支上:
[jonson@localhost mygit]$ git merge dev Updating 7646ab6..e2d7f2d Fast-forward text.txt | 1 + 1 file changed, 1 insertion(+) [jonson@localhost mygit]$ cat text.txt # this is jonson first repo create a new brach
git merge
命令用于合并指定分支到当前分支。合并后,再查看文件的内容,就能够看到,和dev分支的最新提交是彻底同样的。
注意:上面的Fast-forward
信息,Git告诉咱们,此次合并是“快进模式”,也就是直接把master执行dev的当前提交,因此合并速度很是快,固然,也不是每次合并都是Fast-forward
,后面会讲其余方式的合并。
#合并完成后,若是须要删除dev分支,能够执行如下命令:
[jonson@localhost mygit]$ git branch -d dev Deleted branch dev (was e2d7f2d). [jonson@localhost mygit]$ git branch * master #删除后,查看branch,就只剩下master分支了。
小结
Git鼓励大量使用分支:
查看分支:git branch 建立分支:git branch <name> 切换分支:git checkout <name> 建立+切换分支:git checkout -b <name> 合并某分支到当前分支:git merge <name> 删除分支:git branch -d <name>
人生不如意之事十有八九,合并分支每每也不是一路顺风的。
1)准备新的分支(feature1)
[jonson@localhost mygit]$ git checkout -b feature1 Switched to a new branch 'feature1'
修改test.txt文件并提交:
[jonson@localhost mygit]$ echo "create two branch" >> text.txt [jonson@localhost mygit]$ cat text.txt # this is jonson first repo create a new brach create two branch [jonson@localhost mygit]$ git add text.txt [jonson@localhost mygit]$ git commit -m "two branch" [feature1 78292e2] two branch 1 file changed, 1 insertion(+)
切换到master分支:
[jonson@localhost mygit]$ git checkout master Switched to branch 'master' Your branch is ahead of 'origin/master' by 1 commit. (use "git push" to publish your local commits) #git提示咱们当前master分支比远程的master分支超前1个提交。
接下来,在master分支上也进行修改并提交:
[jonson@localhost mygit]$ echo "create a three branch" >> text.txt [jonson@localhost mygit]$ git add text.txt [jonson@localhost mygit]$ git commit -m "three branch" [master e27d699] three branch 1 file changed, 1 insertion(+)
如今,master
分支和feature1
分支各自都分别有新的提交,变成了这样:
这种状况下,Git没法执行“快速合并”,只能试图把各自的修改合并起来,但这种合并就可能会有冲突,咱们试试看:
[jonson@localhost mygit]$ git merge feature1 Auto-merging text.txt CONFLICT (content): Merge conflict in text.txt Automatic merge failed; fix conflicts and then commit the result.
果真冲突了,Git告诉咱们,test.txt文件存在冲突,必须手动解决冲突后再提交。git status
能够告诉咱们冲突的文件:
[jonson@localhost mygit]$ git status # On branch master # Your branch is ahead of 'origin/master' by 2 commits. # # Unmerged paths: # (use "git add/rm <file>..." as appropriate to mark resolution) # # both modified: readme.txt # no changes added to commit (use "git add" and/or "git commit -a")
咱们能够直接查看test.txt的内容:
[jonson@localhost mygit]$ cat text.txt # this is jonson first repo create a new brach <<<<<<< HEAD create a three branch ======= create two branch >>>>>>> feature1
git用‘<<<<<<<’,’=======’, ‘>>>>>>>’标记出不一样分支的内容,咱们修改以下后保存:
[jonson@localhost mygit]$ cat text.txt # this is jonson first repo create a new brach <<<<<<< HEAD create two branch ======= create two branch >>>>>>> feature1
再提交:
[jonson@localhost mygit]$ git commit -m "new branch" [master 966dd22] new branch
如今,master分支和feature1分支变成了下图所示:
工做完成后,删除feature1分支:
[jonson@localhost mygit]$ git branch -d feature1 Deleted branch feature1 (was 78292e2).
小结:
当Git没法自动合并分支时,就必须解决冲突。解决冲突后,再提交,最后合并完成。
一般,合并分支时,若是可能,Git会用Fast forward
模式,但这种模式下,删除分支后,会丢掉分支信息。若是要强制禁用Fast forward
模式,Git就会在merge时生成一个新的commit,这样,从分支历史上就能够看出分支信息。下面来进行实践:
1)首先,仍然建立并切换dev分支:
[jonson@localhost mygit]$ git checkout -b dev Switched to a new branch 'dev' [jonson@localhost mygit]$ git branch * dev master
修改test.txt文件,并提交一个新的commit:
[jonson@localhost mygit]$ echo "create four branch" >> text.txt [jonson@localhost mygit]$ git add text.txt [jonson@localhost mygit]$ git commit -m "add merge" [dev e730d1b] add merge 1 file changed, 1 insertion(+)
如今咱们切换回master:
[jonson@localhost mygit]$ git checkout master Switched to branch 'master'
准备合并dev分支,请注意--no-ff
参数,表示禁用Fast forward
:
[jonson@localhost mygit]$ git merge --no-ff -m "merge with no-ff" dev Merge made by the 'recursive' strategy. text.txt | 1 + 1 file changed, 1 insertion(+)
由于本次合并要建立一个新的commit,因此加上-m参数,把commit描述写进去。合并后,咱们用git log
看看分支历史:
能够看到,不使用Fast forward
模式,merge后就像这样:
小结:
Git分支十分强大,在团队开发中应该充分应用。
合并分支时,加上--no-ff
参数就能够用普通模式合并,合并后的历史有分支,能看出来曾经作过合并,而fast forward
合并就看不出来曾经作过合并。
软件开发中,一定会有bug。有了bug就须要修复,在Git中,因为分支是如此的强大,因此,每一个bug均可以经过一个新的临时分支来修复,修复后,合并分支,而后将临时分支删除。
当你接到一个修复一个代码101的bug的任务时,很天然的,你想建立一个临时分支来修复它, 可是,当前正在dev上进行的工做尚未提交:
[jonson@localhost mygit]$ echo "create five branch" >> text.txt [jonson@localhost mygit]$ git status # On branch dev # 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: text.txt # # Untracked files: # (use "git add <file>..." to include in what will be committed) # # new-repo/ no changes added to commit (use "git add" and/or "git commit -a")
此时,问题出现了,工做只进行到一半,还无法提交,预计完成还需1天时间。可是,必须在两个小时内修复该bug,怎么办?
幸亏,Git还提供了一个stash功能,能够把当前工做现场“储藏”起来,等之后恢复现场后继续工做:
[jonson@localhost mygit]$ git stash Saved working directory and index state WIP on dev: e730d1b add merge HEAD is now at e730d1b add merge
如今,能够查看工做区,能够看到就是干净的了,所以能够放心地建立分支来修复bug。
首先肯定要在哪一个分支上修复bug,假定须要在master分支上修复,就从master建立临时分支(issue-101):
[jonson@localhost mygit]$ git checkout master Switched to branch 'master' Your branch is ahead of 'origin/master' by 2 commits. (use "git push" to publish your local commits) [jonson@localhost mygit]$ git checkout -b issue-101 Switched to a new branch 'issue-101'
如今修复bug,就假如须要把“create four branch”改为“create five branch”,而后提交:
[jonson@localhost mygit]$ sed -i 's/four/five/' text.txt [jonson@localhost mygit]$ git add text.txt [jonson@localhost mygit]$ git commit -m "fix bug 101" [issue-101 6484832] fix bug 101 1 file changed, 1 insertion(+), 1 deletion(-)
修复完成后,切换到master分支,并完成合并,最后再删除这个临时分支:
[jonson@localhost mygit]$ git checkout master [jonson@localhost mygit]$ git merge --no-ff -m "merged bug fix 101" issue-101 Merge made by the 'recursive' strategy. text.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) [jonson@localhost mygit]$ git branch -d issue-101 Deleted branch issue-101 (was 6484832).
这下问题解决了,原计划两个小时的bug修复只花了5分钟!如今,又能够接着回到dev分支干活了:
[jonson@localhost mygit]$ git checkout dev Switched to branch 'dev' [jonson@localhost mygit]$ git status # On branch dev # Untracked files: # (use "git add <file>..." to include in what will be committed) # # new-repo/ nothing added to commit but untracked files present (use "git add" to track)
工做区此时是干净的,由于刚才的工做现场咱们用git stash
储藏起来了,能够用git stash list
命令看看:
[jonson@localhost mygit]$ git stash list stash@{0}: WIP on dev: e730d1b add merge
因此如今咱们须要恢复一下,有两种方式:
一种是用git stash apply
恢复,可是回复后,stash内容并删除,你还须要用git stash drop
来删除。
另外一种是用git starh pop,恢复的同时把stash内容也删了(因此通常采用这种方法):
再用git stash list 查看,就看不到stash内容了,而且也恢复到以前的工做状态:
#当你屡次stash时,若是你要恢复指定的stash,用如下命令:git stash apply stash@{0}
小结:
修复bug时,咱们会经过建立新的bug分支进行修复,而后合并,最后删除。
当手头工做没有完成时,先把工做现场git stash
储藏起来,而后去修复bug,再git stash pop
,删除stash内容,并恢复到工做现场。
当你从远程库克隆时,实际上Git自动把本地master分支和远程的master分支对应起来了,而且,远程仓库的默认名称是origin。
#要查看远程库的详细信息,用`git remote -v`: [jonson@localhost mygit]$ git remote -v origin git@github.com:sqm-sys/jonson-repo.git (fetch) origin git@github.com:sqm-sys/jonson-repo.git (push)
上面显示了能够抓取和推送的origin的地址。若是没有推送权限,就看不到push的地址。
推送分支
推送分支,就是把该分支上的全部本地提交推送到远程库。推送时,要指定本地分支,这样,Git就会把该分支推送到远程库对应的远程分支上:
[jonson@localhost mygit]$ git push origin master 推送其余分支: [jonson@localhost mygit]$ git push origin dev
可是,并非必定要把本地分支往远程推送,那么,哪些分支须要推送,哪些不须要呢?
抓取分支
多人协做时,你们都会往master和dev分支上推送各自的修改。
如今,模拟一个你的同事,能够在另外一台电脑(注意要把SSH key添加获得GitHub)下克隆:
[zhangsan@localhost ~]$ git clone git@github.com:sqm-sys/jonson-repo.git Cloning into 'jonson-repo'... remote: Enumerating objects: 26, done. remote: Counting objects: 100% (26/26), done. remote: Compressing objects: 100% (12/12), done. remote: Total 26 (delta 5), reused 25 (delta 4), pack-reused 0 Receiving objects: 100% (26/26), done. Resolving deltas: 100% (5/5), done.
须要的注意的是,你的同事从远程库clone时,默认状况下,只能看到本地的master分支,而看不到其余分支:
[zhangsan@localhost ~]$ cd jonson-repo/ [zhangsan@localhost jonson-repo]$ git branch * master
如今,你的同事要在dev分支上开发,就必须建立远程origin的dev分支到本地(能够执行如下命令):
[zhangsan@localhost jonson-repo]$ git checkout -b dev origin/dev Branch dev set up to track remote branch dev from origin. Switched to a new branch 'dev'
此时,他就能够在dev上分支上进行修改了,而后,时不时的把dev分支push到远程:
[zhangsan@localhost jonson-repo]$ echo "create six branch" >> text.txt [zhangsan@localhost jonson-repo]$ git add text.txt [zhangsan@localhost jonson-repo]$ git commit -m "add six branch" [dev f50cefa] add six branch 1 file changed, 1 insertion(+) [zhangsan@localhost jonson-repo]$ git push origin dev Counting objects: 5, done. Delta compression using up to 2 threads. Compressing objects: 100% (2/2), done. Writing objects: 100% (3/3), 287 bytes | 0 bytes/s, done. Total 3 (delta 1), reused 0 (delta 0) remote: Resolving deltas: 100% (1/1), completed with 1 local object. To git@github.com:sqm-sys/jonson-repo.git c7d965a..f50cefa dev -> dev
你的同事已经向origin/dev分支推送了他的提交,而此时你也对一样的文件做了修改,并试图推送:
[jonson@localhost mygit]$ git branch * dev master [jonson@localhost mygit]$ echo "ha ha ha " >> text.txt [jonson@localhost mygit]$ git add text.txt [jonson@localhost mygit]$ git commit -m "this is haha" [dev 94c4cde] this is haha 1 file changed, 1 insertion(+) [jonson@localhost mygit]$ git push origin dev #进行推送 To git@github.com:sqm-sys/jonson-repo.git ! [rejected] dev -> dev (fetch first) error: failed to push some refs to 'git@github.com:sqm-sys/jonson-repo.git' hint: Updates were rejected because the remote contains work that you do hint: not have locally. This is usually caused by another repository pushing hint: to the same ref. You may want to first merge the remote changes (e.g., hint: 'git pull') before pushing again. hint: See the 'Note about fast-forwards' in 'git push --help' for details.
推送失败,缘由是你的同事的最新提交和你试图推送的提交有冲突,解决办法也很简单,Git已经提示咱们:先用git pull
把最新的提交从origin/dev
抓下来,而后,在本地合并,解决冲突,再推送:
能够看到git pull
失败了,缘由是没有指定本地dev分支与远程origin/dev分支的连接,根据提示,设置dev和origin/dev的连接:
[jonson@localhost mygit]$ git branch --set-upstream-to=origin/dev Branch dev set up to track remote branch dev from origin.
建立连接后,在执行pull:
[jonson@localhost mygit]$ git pull Auto-merging text.txt CONFLICT (content): Merge conflict in text.txt Automatic merge failed; fix conflicts and then commit the result.
这回git pull
成功,可是合并有冲突,须要手动解决,解决的方法以前的解决冲突彻底同样(将冲突的内容进行修改)。解决后,提交,再push:
[jonson@localhost mygit]$ git add text.txt [jonson@localhost mygit]$ git commit -m "merge & six branch" [dev 4ba822d] merge & six branch [jonson@localhost mygit]$ git push origin dev Counting objects: 10, done. Delta compression using up to 2 threads. Compressing objects: 100% (4/4), done. Writing objects: 100% (6/6), 616 bytes | 0 bytes/s, done. Total 6 (delta 1), reused 0 (delta 0) remote: Resolving deltas: 100% (1/1), done. To git@github.com:sqm-sys/jonson-repo.git f50cefa..4ba822d dev -> dev
push成功,所以,多人协做的工做模式一般是这样:
1)首先,能够试图用'git push origin <branch-name>' 推送本身的修改;
2)若是推送失败,则由于远程分支比你的本地更新,须要先用git pull
试图合并;
3)若是合并冲突,则解决冲突,并在本地提交;
4) 没有冲突或者解决掉冲突后,再用'git push origin <branch-name>' 推送就能成功;
若是git pull
提示“no tracking information”,则说明本地分支和远程分支的连接关系没有建立,用命令'git branch --set-upstream-to=origin/<branch-name> '。这就是多人协做的工做模式。。。