1,分支管理
分支在实际中有什么用呢?假设你准备开发一个新功能,但是需要两周才能完成,第一周你写了50%的代码,立刻提交,由于代码还没有写完,不完整的代码库会导致别人不能干活了。如果等代码全部写完再一次提交,又存在丢失每天进度的风险。
现在有了分支,就不能怕了,你创建了一个属于你自己的分支,别人看不到,还继续在原来的分支上正常工作,而你在自己的分支上干活,想提交就提交,直到开发完毕后,在一次性合并到原来的分支上,这样,既安全,又不影响别人工作。
Git的分支与其他版本控制系统不同,无论创建,切换和删除分支,Git在1秒钟之内就能完成!无论你的版本库是1个文件还是1万个文件。
2,创建与合并分支
在版本回退里,我们已经知道,每次提交,Git都把它们串成一条时间线,这条时间线就是一个分支。截止到目前,只有一条时间线,在Git里,这个分支叫主分支(master分支),HEAD严格来说不是指向提交,而是指向master,master才是指向提交的,所以,HEAD指向的就是当前分支。
① 一开始的时候,master分支是一条线,Git用master指向最新的提交,再用HEAD执行maser,就能确定当前分支,以及当前分支的提交点,每次提交,master分支都会向前一步:
② 当我们创建新的分支,例如dev时,Git创建了一个指针叫dev,指向master相同的提交,再把HEAD指向dev,就表示当前分支在dev上:
可以看到,Git创建一个分支很快,因为除了增加一个dev指针,改改HEAD的指向,工作区的文件都没有任何变化。
③ 不过,从现在开始,对工作区的修改和提交就是针对dev分支了,比如新提交一次后,dev指针往前移动一步,而master指针不变:
④ 假如我们在dev上的工作完成了,就可以把dev合并到master上。Git合并很简单,就是把master指向dev的当前提交,就完成了合并:
所以Git合并分支也很快,就改改指针,工作区内容不变。
⑤ 合并完分支后,甚至可以删除dev分支。删除dev分支就是把dev指针给删掉,我们就剩下了一条master分支:
下面开始进行实践:
首先,我们创建dev分支,然后切换到dev分支:
[jonson@localhost mygit]$ git checkout -b dev
Switched to a new branch 'dev'
#git checkout命令加上-b参数表示创建并切换,相当于以下两条命令:
$ git branch dev
$ git checkout dev
然后,用git branch命令查看当前分支:
[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>
3,解决冲突
人生不如意之事十有八九,合并分支往往也不是一帆风顺的。
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无法自动合并分支时,就必须解决冲突。解决冲突后,再提交,最后合并完成。
4,分支管理策略
通常,合并分支时,如果可能,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
合并就看不出来曾经做过合并。
5,Bug分支
软件开发中,必定会有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内容,并恢复到工作现场。
6,多人协作
当你从远程库克隆时,实际上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 分支是开发分支,团队所有成员都需要在上面工作,所以也需要与远程同步;
- bug分支只用于在本地修复bug,就没必要推到远程了,除非领导要看看你每周到底修复了几个bug;
抓取分支
多人协作时,大家都会往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> '。
这就是多人协作的工作模式。。。
来源:51CTO
作者:wx5b9c94b17c62a
链接:https://blog.51cto.com/13972012/2485046