测试开发实战|Git分支管理实操,搞定在线合并和本地合并

原创 月关

image

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

image

特性分支工作流(Feature Branch Workflow)

以Bitbucket的官方文档的实例作为简单介绍:

例如Mary现在想要开发,在开发前她可以通过checkout命令建立一个新的分支:

image

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

git checkout -b marys-feature master

然后Mary可以在这个本地进行相关的更改:


git status
git add <some-file>
git commit

接着她可以不断将本地修改上传至特性分支的中心仓库中,直到自己全完修改完成

image

git push -u origin marys-feature

image


git push

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

image

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

image

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

image

git checkout master
git pull
git pull origin marys-feature
git push

Git flow

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

在GitHub上进行基本的演示(实际工作中,公司用的还是GitLab较多,后面会有总结演示)

1.1) 先使用 git checkout -b 命令来创建一个新的分支并切换到此分支中去,用 git branch 命令可查看当前所处分支:


$ git checkout -b gitTestBranch
Switched to a new branch 'gitTestBranch'

$ git branch
* gitTestBranch
  master

1.2) 将 readme.txt 文件最后一行加入如下内容并 commit


I am a test engineer.
I want to study Git.
branch gitTestBranch update

1.3) push 到远程仓库并查看分支,首次push需要用 git push -ugit push --set-upstream 命令设置上下游的关联关系:

在GitHub上查看master分支和gitTestBranch分支的对比,可见gitTestBranch已成功push:

master

image

image

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

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

image

没有 conflict ,可以直接 merge

image

这个时候再看 master 分支,就已经被成功合并了

image

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

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

I am a test engineer.
I want to study Git.
branch gitTestBranch update1
branch gitTestBranch update2

git commit -a -m "gitTestBranch second update"
git push

image

2.2)通过fetch将gitTestBranch分支拿下来到本地,修改本地文件并合并
修改本地gitTestBranch分支,修改加入“ branch gitTestBranch update3 ”并提交到远程分支


vi readme.txt

I am a test engineer.
I want to study Git.
branch gitTestBranch update1
branch gitTestBranch update2
branch gitTestBranch update3

$ git commit -a -m "third update"
$ git push

2.3)master分支上 fetch 拿取远程 gitTestBranch 分支,修改冲突,合并提交


$ git checkout master
$ git fetch origin gitTestBranch
$ git merge origin/gitTestBranch
# fix conflict
$ git commit -a -m "fix conflict"
$ git push

2.4)这时候在GitHub上进行查看:
commit历史中可见提交记录:

image

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

image

参考链接:

git的基本使用流程

特性分支工作流

gitlab工作流

https://docs.gitlab.com/ee/workflow/gitlab_flow.html

多种工作流对比

gitlab私服搭建