Git 实战(四)| Git 分支管理实操,搞定在线合并和本地合并
类似于 SVN 这种集中式版本管理,三年前刚来上海工作时候,在华为驻场上班,华为用的就是 SVN,印象最深的就是那个小乌龟的图标;后来到外面工作,渐渐发现用 Git 的非常多,慢慢学习了解发现 Git 这种分布式的版本管理确实很好很强大,后面也就重点学习 Git 的分支管理策略了(其实 SVN 我现在压根就不会了,哈哈。。。)

centralized workflows
特性分支工作流(Feature Branch Workflow)
以 Bitbucket 的官方文档的实例作为简单介绍:
例如 Mary 现在想要开发,在开发前她可以通过 checkout 命令建立一个新的分支:
Feature Branch Workflow: comit changes
Before she starts developing a feature, Mary needs an isolated branch to work on. She can request a new branch with the following command
然后 Mary 可以在这个本地进行相关的更改:
接着她可以不断将本地修改上传至特性分支的中心仓库中,直到自己全完修改完成

然后,她在 git gui(GitHub 或 GitLab 等)中提交pull
请求,请求将 marys 特性合并到 master 中,团队成员将自动收到通知。

Mary 的同事 Bill 收到了pr
,Bill 觉得在合并到正式项目中之前还需要做一些修改,于是在pr
的回复中对 Mary 进行告知,接着 Mary 继续修改开发,完成后再次提交 pr:

一旦 Bill 准备接受pull request
,有人需要将该特征merge
到稳定的项目中(这可以由 Bill 或 Mary 来完成)

Git flow

特性分支操作演示,合并方式一:在线合并

在 GitHub 上进行基本的演示(实际工作中,公司用的还是 GitLab 较多,后面会有总结演示)
1.1) 先使用git checkout -b
命令来创建一个新的分支并切换到此分支中去,用git branch
命令可查看当前所处分支:
1.2) 将readme.txt
文件最后一行加入如下内容并commit
1.3) push
到远程仓库并查看分支,首次 push 需要用git push -u
或git push --set-upstream
命令设置上下游的关联关系:
在 GitHub 上查看 master 分支和 gitTestBranch 分支的对比,可见 gitTestBranch 已成功 push:
master:

gitTestBranch:

1.4) 使用git log --graph --all --decorate=short
命令可以查看提交的分支走向,如果分支较多的话就会出现如下效果:

1.5)这个时候我们可以通过pr
对分支进行merge
:发起pr

没有conflict
,可以直接merge
这个时候再看master
分支,就已经被成功合并了

特性分支操作演示,合并方式二:本地合并

2.1) 先在 readme.txt 文件中加入一行branch gitTestBranch update2
,然后提交到远程分支中:

2.2)通过 fetch 将 gitTestBranch 分支拿下来到本地,修改本地文件并合并修改本地 gitTestBranch 分支,修改加入“branch gitTestBranch update3
”并提交到远程分支
2.3)master 分支上fetch
拿取远程gitTestBranch
分支,修改冲突,合并提交
2.4)这时候在 GitHub 上进行查看:commit 历史中可见提交记录:

检查 master,发现已经被成功合并

关注我公众号 TestingStudio,霍格沃兹的干货都很硬核
评论