前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Git实战(四)| Git分支管理实操,搞定在线合并和本地合并

Git实战(四)| Git分支管理实操,搞定在线合并和本地合并

原创
作者头像
霍格沃兹测试开发
发布2022-04-26 14:21:00
2860
发布2022-04-26 14:21:00
举报
文章被收录于专栏:测吧测试开发

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

centralized workflows

以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

代码语言:javascript
复制
git checkout -b marys-feature master

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

代码语言:javascript
复制
git status
git add <some-file>
git commit

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

代码语言:javascript
复制
git push -u origin marys-feature
代码语言:javascript
复制
git push

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

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

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

代码语言:javascript
复制
git checkout master
git pull
git pull origin marys-feature
git push

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

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

代码语言:javascript
复制
$ git checkout -b gitTestBranch
Switched to a new branch 'gitTestBranch'

$ git branch
* gitTestBranch
  master

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

代码语言:javascript
复制
I am a test engineer.
I want to study Git.
branch gitTestBranch update

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,然后提交到远程分支中:

代码语言:javascript
复制
I am a test engineer.
I want to study Git.
branch gitTestBranch update1
branch gitTestBranch update2
代码语言:javascript
复制
git commit -a -m "gitTestBranch second update"
git push

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

代码语言:javascript
复制
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分支,修改冲突,合并提交

代码语言:javascript
复制
$ 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历史中可见提交记录:

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

参考链接:

git的基本使用流程

Atlassian

Set up a git repository: git init creates a new repo, git clone copies an existing repo, git config configures your Git installation from the command line

特性分支工作流

Atlassian

Learn if this Git branching model is right for you and your team with this deep dive into the Git Feature Branch Workflow.

gitlab工作流 https://docs.gitlab.com/ee/workflow/gitlab_flow.html 多种工作流对比

Atlassian

A brief introduction to common Git workflows including the Centralized Workflow, Feature Branch Workflow, Gitflow Workflow, and Forking Workflow.

gitlab私服搭建

docs.gitlab.com

Documentation for GitLab Community Edition, GitLab Enterprise Edition, Omnibus GitLab, and GitLab Runner.

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档