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

简介: ![](https://ceshiren.com/uploads/default/original/3X/5/7/577bcc2d8a6d08b63c42c9cf7d62a83d7dec13ea.gif)![](https://ceshiren.com/uploads/default/original/3X/e/8/e8d7d4ae9c89bd6b22633c2718d97a2598a29a4
更多技术文章分享和免费资料领取
https://qrcode.ceba.ceshiren.com/link?name=article&project_id=qrcode&from=Aliyun&timestamp=1651203752

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


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

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的基本使用流程
https://www.atlassian.com/git/tutorials/setting-up-a-repository
特性分支工作流
https://www.atlassian.com/git/tutorials/comparing-workflows/feature-branch-workflow
gitlab工作流
https://docs.gitlab.com/ee/workflow/gitlab_flow.html
多种工作流对比
https://www.atlassian.com/git/tutorials/comparing-workflows
gitlab私服搭建
https://docs.gitlab.com/omnibus/docker/

原文链接

https://qrcode.ceba.ceshiren.com/link?name=article&project_id=qrcode&from=Aliyun&timestamp=1651203752

相关文章
|
14天前
|
API 开发工具 git
《Git 简易速速上手小册》第3章:分支管理(2024 最新版)
《Git 简易速速上手小册》第3章:分支管理(2024 最新版)
39 1
|
2月前
|
开发工具 git
|
2月前
|
安全 开发工具 git
【Git】—— 分支管理策略
【Git】—— 分支管理策略
|
8天前
|
jenkins 持续交付 开发工具
Jenkins 快速入门 (含Jenkins + Docker + Git 简单的自动化实操)(下)
Jenkins 快速入门 (含Jenkins + Docker + Git 简单的自动化实操)
|
8天前
|
jenkins Java 持续交付
Jenkins 快速入门 (含Jenkins + Docker + Git 简单的自动化实操)(上)
Jenkins 快速入门 (含Jenkins + Docker + Git 简单的自动化实操)
|
14天前
|
开发工具 git
git 拉取代码仓库代码报错(合并错误 refusing to merge unrelated histories)
git 拉取代码仓库代码报错(合并错误 refusing to merge unrelated histories)
20 0
|
25天前
|
数据可视化 持续交付 开发工具
实际项目中如何使用Git做分支管理
实际项目中如何使用Git做分支管理
|
2月前
|
Shell 开发工具 git
【问题篇】git创建分支后idea切换分支找不到以及合并问题
【问题篇】git创建分支后idea切换分支找不到以及合并问题
30 0
|
4月前
|
数据可视化 安全 开发工具
git的分支的使用,创建分支,合并分支,删除分支,合并冲突,分支管理策略,bug分支,强制删除分支
git的分支的使用,创建分支,合并分支,删除分支,合并冲突,分支管理策略,bug分支,强制删除分支

相关实验场景

更多