1、Centralized workflows
类似于SVN这种集中式版本管理,三年前刚来上海工作时候,在华为驻场上班,华为用的就是SVN,印象最深的就是那个小乌龟的图标;后来到外面工作,渐渐发现用Git的非常多,慢慢学习了解发现Git这种分布式的版本管理确实很好很强大,后面也就重点学习Git的分支管理策略了(其实SVN我现在压根就不会了,哈哈。。。)2、特性分支工作流(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 commandgit checkout -b marys-feature master
然后Mary可以在这个本地进行相关的更改:
git status
git add <some-file>
git commit
接着她可以不断将本地修改上传至特性分支的中心仓库中,直到自己全完修改完成
git push -u origin marys-feature
git push
然后,她在git gui(GitHub或GitLab等)中提交pull
请求,请求将marys特性合并到master中,团队成员将自动收到通知。
pr
,Bill觉得在合并到正式项目中之前还需要做一些修改,于是在pr
的回复中对Mary进行告知,接着Mary继续修改开发,完成后再次提交pr:
一旦Bill准备接受pull request
,有人需要将该特征merge
到稳定的项目中(这可以由Bill或Mary来完成)
git checkout master
git pull
git pull origin marys-feature
git push
3、Git flow
4、特性分支操作演示,合并方式一:在线合并
在GitHub上进行基本的演示(实际工作中,公司用的还是GitLab较多,后面会有总结演示)
- 4.1) 先使用
git checkout -b
命令来创建一个新的分支并切换到此分支中去,用git branch
命令可查看当前所处分支:
$ git checkout -b gitTestBranch
Switched to a new branch 'gitTestBranch'
$ git branch
* gitTestBranch
master
- 4.2) 将
readme.txt
文件最后一行加入如下内容并commit
I am a test engineer.
I want to study Git.
branch gitTestBranch update1
$ git commit -a -m "update readme"
[gitTestBranch dbd9bfe] update readme
1 file changed, 1 insertion(+)
$ git log
commit dbd9bfe50839330f3d0cbf72546688164d22f67a (HEAD -> gitTestBranch, origin/gitTestBranch)
Author: qinzhen666 <376057520@qq.com>
Date: Sun Sep 22 15:47:13 2019 +0800
update readme
- 4.3)
push
到远程仓库并查看分支,首次push需要用git push -u
或git push --set-upstream
命令设置上下游的关联关系:
$ git push --set-upstream origin gitTestBranch
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 8 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 307 bytes | 307.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
remote:
remote: Create a pull request for 'gitTestBranch' on GitHub by visiting:
remote: https://github.com/xxx/gitDemo/pull/new/gitTestBranch
remote:
To github.com:xxx/gitDemo.git
* [new branch] gitTestBranch -> gitTestBranch
Branch 'gitTestBranch' set up to track remote branch 'gitTestBranch' from 'origin'.
在GitHub上查看master分支和gitTestBranch分支的对比,可见gitTestBranch已成功push: master:
gitTestBranch:- 4.4) 使用
git log --graph --all --decorate=short
命令可以查看提交的分支走向,如果分支较多的话就会出现如下效果: - 4.5)这个时候我们可以通过
pr
对分支进行merge
: 发起pr
没有conflict
,可以直接merge
这个时候再看master
分支,就已经被成功合并了
5、特性分支操作演示,合并方式二:本地合并
- 5.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
- 5.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
- 5.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
- 5.4)这时候在GitHub上进行查看: commit历史中可见提交记录: 检查master,发现已经被成功合并
参考链接:
- git的基本使用流程 www.atlassian.com/git/tutoria…
- 特性分支工作流 www.atlassian.com/git/tutoria…
- gitlab工作流 docs.gitlab.com/ee/workflow…
- 多种工作流对比 www.atlassian.com/git/tutoria…
- gitlab私服搭建 docs.gitlab.com/omnibus/doc…