zl程序教程

您现在的位置是:首页 >  工具

当前栏目

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

Git 管理 实战 分支 本地 在线 合并 搞定
2023-09-11 14:14:51 时间

900×400 65.6 KB


类似于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

git 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中,团队成员将自动收到通知。

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


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

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

1080×1454 147 KB


在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

1080×258 25.7 KB

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

1080×359 48 KB

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


gitTestBranch:

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

909×857 106 KB

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


没有conflict,可以直接merge

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

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

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历史中可见提交记录:

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

参考链接:

git的基本使用流程

Atlassian

Setting up a repository | Atlassian Git Tutorial

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

Git Feature Branch Workflow | Atlassian Git Tutorial

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

Git Workflow | Atlassian Git Tutorial

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

gitlab私服搭建

docs.gitlab.com

GitLab Docker images | GitLab

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

AUTHOR

月关,霍格沃兹测试学院优秀学员。一个在质量保障领域攀登探索的tester,致力于用技术改变身边人对测试的认识。对Web,接口和APP 自动化均有所涉及。