分支

首先,我们创建dev分支,然后切换到dev分支:
$ git checkout -b dev Switched to a new branch 'dev'
git checkout命令加上-b参数表示创建并切换,相当于以下两条命令:
$ git branch dev $ git checkout dev Switched to branch 'dev'
然后,用git branch命令查看当前分支:
$ git branch * dev master
git branch命令会列出所有分支,当前分支前面会标一个*号。

然后,我们就可以在dev分支上正常提交,比如对readme.txt做个修改,加上一行:
Creating a new branch is quick.
然后提交:
$ git add readme.txt $ git commit -m "branch test" [dev b17d20e] branch test 1
file changed, 1 insertion(+)
现在,dev分支的工作完成,我们就可以切换回master分支:
$ git checkout master Switched to branch 'master'
切换回master分支后,再查看一个readme.txt文件,刚才添加的内容不见了!因为那个提交是在dev分支上,而master分支此刻的提交点并没有变:

现在,我们把dev分支的工作成果合并到master分支上:
$ git merge dev Updating d46f35e..b17d20e Fast-forward readme.txt | 1 + 1 file
changed, 1 insertion(+)
git merge命令用于合并指定分支到当前分支。合并后,再查看readme.txt的内容,就可以看到,和dev分支的最新提交是完全一样的。

本地分支冲突

准备新的feature1分支,继续我们的新分支开发:
$ git switch -c feature1 Switched to a new branch 'feature1'
修改readme.txt最后一行,改为:
Creating a new branch is quick AND simple.
在feature1分支上提交:
$ git add readme.txt $ git commit -m "AND simple" [feature1 14096d0] AND
simple 1 file changed, 1 insertion(+), 1 deletion(-)
切换到master分支:
$ git switch 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分支上把readme.txt文件的最后一行改为:
Creating a new branch is quick & simple.
提交:
$ git add readme.txt $ git commit -m "& simple" [master 5dc6824] & simple 1
file changed, 1 insertion(+), 1 deletion(-)
现在,master分支和feature1分支各自都分别有新的提交,变成了这样:

这种情况下,Git无法执行“快速合并”,只能试图把各自的修改合并起来,但这种合并就可能会有冲突,我们试试看:
$ git merge feature1 Auto-merging readme.txt CONFLICT (content): Merge
conflict in readme.txt Automatic merge failed; fix conflicts and then commit
the result.
Git告诉我们,readme.txt文件存在冲突,必须手动解决冲突后再提交。git status也可以告诉我们冲突的文件:
$ git status On branch master Your branch is ahead of 'origin/master' by 2
commits. (use "git push" to publish your local commits) You have unmerged
paths. (fix conflicts and run "git commit") (use "git merge --abort" to abort
the merge) Unmerged paths: (use "git add <file>..." to mark resolution) both
modified: readme.txt no changes added to commit (use "git add" and/or "git
commit -a")
我们可以直接查看readme.txt的内容:
Git is a distributed version control system. Git is free software distributed
under the GPL. Git has a mutable index called stage. Git tracks changes of
files. <<<<<<< HEAD Creating a new branch is quick & simple. ======= Creating a
new branch is quick AND simple. >>>>>>> feature1
Git用<<<<<<<,=======,>>>>>>>标记出不同分支的内容,我们修改如下后保存:
Creating a new branch is quick and simple.
再提交:
$ git add readme.txt $ git commit -m "conflict fixed" [master cf810e4]
conflict fixed
github分支

当你从远程仓库克隆时,实际上Git自动把本地的master分支和远程的master分支对应起来了,并且,远程仓库的默认名称是origin
。要查看远程库的信息,用git remote:
$ git remote origin
或者,用git remote -v显示更详细的信息:
$ git remote -v origin git@github.com:yourname/learngit.git (fetch) origin
git@github.com:yourname/learngit.git (push)
上面显示了可以抓取和推送的origin的地址。如果没有推送权限,就看不到push的地址。

推送分支

推送分支,就是把该分支上的所有本地提交推送到远程库。推送时,要指定本地分支,这样,Git就会把该分支推送到远程库对应的远程分支上:
$ git push origin master
如果要推送其他分支,比如dev,就改成:
$ git push origin dev
但是,并不是一定要把本地分支往远程推送,那么,哪些分支需要推送,哪些不需要呢?

*
master分支是主分支,因此要时刻与远程同步;

*
dev分支是开发分支,团队所有成员都需要在上面工作,所以也需要与远程同步;

*
bug分支只用于在本地修复bug,就没必要推到远程了,除非老板要看看你每周到底修复了几个bug;

*
feature分支是否推到远程,取决于你是否和你的小伙伴合作在上面开发。

多人分支冲突

多人协作的工作模式通常是这样:

*
首先,可以试图用git push origin <branch-name>推送自己的修改;

*
如果推送失败,则因为远程分支比你的本地更新,需要先用git pull试图合并;

*
如果合并有冲突,则解决冲突,并在本地提交;

*
没有冲突或者解决掉冲突后,再用git push origin <branch-name>推送就能成功!

如果git pull提示no tracking information,则说明本地分支和远程分支的链接关系没有创建,用命令git branch
--set-upstream-to <branch-name> origin/<branch-name>。

实战:

如果你的小伙伴已经向origin/dev分支推送了他的提交,而碰巧你也对同样的文件作了修改,并试图推送:
$ git push origin dev To github.com:yourname/learngit.git ! [rejected] dev ->
dev (non-fast-forward) error: failed to push some refs to
'git@github.com:yourname/learngit.git' hint: Updates were rejected because the
tip of your current branch is behind hint: its remote counterpart. Integrate
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 There is no tracking information for the current branch. Please
specify which branch you want to merge with. See git-pull(1) for details. git
pull <remote> <branch> If you wish to set tracking information for this branch
you can do so with: git branch --set-upstream-to=origin/<branch> dev
git pull也失败了,原因是没有指定本地dev分支与远程origin/dev分支的链接,根据提示,设置dev和origin/dev的链接:
$ git branch --set-upstream-to=origin/dev dev Branch 'dev' set up to track
remote branch 'dev' from 'origin'.
再pull:
$ git pull Auto-merging env.txt CONFLICT (add/add): Merge conflict in env.txt
Automatic merge failed; fix conflicts and then commit the result.
 

技术
©2019-2020 Toolsou All rights reserved,
数字滚动抽奖小程序VaR - 风险价值 - 蒙特卡罗法 - Python百度网盘偷偷更新,终于实现免费不限速了! Chrome OS,对程序员和Windows意味着什么?,互联网营销华为Mate 40 Pro+ 5G曝光:徕卡电影镜头、陶瓷机身Qt学习2——.pro文件和.h文件介绍python:将一个文件转换为二进制文件(binary)第十一届蓝桥杯C/C++ 大学 B 组大赛软件类省赛网站手机号码抓取方式蚂蚁集团香港IPO获得中国证监会批准