Git 分支相关操作

news2024/12/26 23:03:59

1 创建一个分支

Create a new directory and initialize a Git repository. We are going to create a directory named “tutorial”.

$ mkdir tutorial
$ cd tutorial
$ git init
Initialized empty Git repository in /Users/eguchi/Desktop/tutorial/.git/

进入这个tutorial文件夹,创建一个文件“myfile.txt”,在其中添加如下文本:

Git commands even a monkey can understand

然后执行如下的addcommit命令:

$ git add myfile.txt
$ git commit -m "first commit"
[master (root-commit) a73ae49] first commit
1 files changed, 1 insertions(+), 0 deletions(-)
create mode 100644 myfile.txt

At this point, the history tree should look like this.

在这里插入图片描述

Let’s create a new branch with the name “issue1”.

$ git branch issue1

If you do not specify any parameters, the branch command will list all branches that correspond to this repository. The asterisk indicates the current active branch.

$ git branch
      issue1
      * master

At this point, the history tree should look like this.

在这里插入图片描述

2 切换分支

Switch over to the branch “issue1” when you want to add new commits to it.

$ git checkout issue1
Switched to branch 'issue1'

This history tree should look like this at the moment.

在这里插入图片描述

Once you are on the “issue1” branch, you can start adding commits to it.

编辑我们刚才创建的myfile.txt文件,在其中添加一行,使得整个文件变为如下两行:

Git commands even a monkey can understand
add: Register a change in an index

Let’s add the bold text below to myfile.txt and commit the change.

$ git add myfile.txt
$ git commit -m "append description of the add command"
[issue1 b2b23c4] append description of the add command1 files changed, 1 insertions(+), 0 deletions(-)

Our history tree will now look like this.

在这里插入图片描述

3 分支合并

Let’s merge “issue1” with “master”

我们可以使用如下的命令来合并分支:

$ git merge

By running the command above, the specified commit will be merged to the current active branch. Most of the time, you will want to merge a branch with the current active branch and you can do so by passing in the branch name in .

To merge commits into the master branch, let’s now switch over to the master branch.

$ git checkout master
Switched to branch 'master'

在合并前,查看myfile.txt文件,可以看到其内容如下:

Git commands even a monkey can understand

可以发现,我们在issue1分支上进行的修改并没有出现master分支上的myfile.txt中。

此时,我们执行merge操作:

$ git merge issue1
Updating 1257027..b2b23c4
Fast-forward
myfile.txt |    1 +
1 files changed, 1 insertions(+), 0 deletions(-)

The position of the master branch will now move over to that of “issue1”. A fast-forward merge has been executed here.

在这里插入图片描述

此时,查看myfile.txt文件:

Git commands even a monkey can understand
add: Register a change in an index

此时可以看到,我们应用在issue1分支上的修改出现在了master分支上。

4 删除分支

Now that “issue1” has been successfully merged with “master”, we can delete it.

We can delete a branch by calling the branch command and passing in the -d option, followed by the branch name. 如下的命令可以删除issue1分支:

$ git branch -d issue1
Deleted branch issue1 (was b2b23c4).

5 并行操作

Branching allows us to work in multiple parallel workspaces.

Let’s create two branches. Create one with the name “issue2″ and another with the name”issue3”, then switch over to “issue2”.

$ git branch issue2
$ git branch issue3
$ git checkout issue2
Switched to branch 'issue2'
$ git branch
  * issue2
    issue3
    master

在这里插入图片描述

修改myfile.txt文件,在下面添加一行,使得其内容变为:

Git commands even a monkey can understand
add: Register a change in an index
commit: Save the status of an index

然后执行如下操作:

$ git add myfile.txt
$ git commit -m "append description of the commit command"
[issue2 8f7aa27] append description of the commit command
1 files changed, 2 insertions(+), 0 deletions(-)

此时各分支状态如下:

在这里插入图片描述

我们切换到issue3分支:

$ git checkout issue3
Switched to branch 'issue3'

“issue3” currently has the same history/content as the master branch. It will not include the recent change that we have just made. This is because the recent change has been commited to the “issue2” branch.

此时修改myfile.txt文件,添加一行修改为:

Git commands even a monkey can understand
add: Register a change in an index
pull: Obtain the content of the remote repository

然后提交这次修改:

$ git add myfile.txt
$ git commit -m "append description of the pull command"
[issue3 e5f91ac] append description of the pull command
1 files changed, 2 insertions(+), 0 deletions(-)

此时分支状态如下:

在这里插入图片描述

We have now added two different line of texts to two different branches in parallel.

6 解决分支冲突

Now let’s merge branches “issue2” and “issue3” into the master branch.

We will switch over to “master” and merge “issue2” with it.

$ git checkout master
Switched to branch 'master'

$ git merge issue2
Updating b2b23c4..8f7aa27
Fast-forward
myfile.txt |    2 ++
1 files changed, 2 insertions(+), 0 deletions(-)

A fast-forward merge has now been executed.

在这里插入图片描述

Let’s try to merge “issue3” into “master”.

$ git merge issue3
Auto-merging myfile.txt
CONFLICT (content): Merge conflict in myfile.txt
Automatic merge failed; fix conflicts and then commit the result.

Git has identified a conflict and will not allow you to automatically merge “issue3” with “master”. The conflict here pertains to the same line on myfile.txt with different content on both branches.

由于我们尝试合并时发生的conflict,此时myfile.txt文件变为如下内容:

Git commands even a monkey can understand
add: Register a change in an index
<<<<<<< HEAD
commit: Save the status of an index
=======
pull: Obtain the content of a remote repository
>>>>>>> issue3

You will find some strange markers being added to myfile.txt. These markers were added by Git, giving us information regarding which line of the file is related to the conflict.

我们可以手动修改冲突,将myfile.txt文件修改为如下内容:

Git commands even a monkey can understand
add: Register a change in an index
commit: Save the status of an index
pull: Obtain the content of a remote repository

Once we are done with resolving the conflict, let’s commit the change.

$ git add myfile.txt
$ git commit -m "merge issue3 branch"
# On branch master
nothing to commit (working directory clean)

此时文件内容为:

Git commands even a monkey can understand
add: Register a change in an index
commit: Save the status of an index
pull: Obtain the content of a remote repository

The revision history will now look like the one below. A new merge commit has been created as a result of the conflict resolution. The master branch is now pointing to the latest merge commit. This is a non fast-forward merge.

在这里插入图片描述

整个rebase的过程是错误的,需要重新整理

7 Rebase a branch

Another approach we can take to integrate “issue3” branch into the master branch is by using the rebase command. Using rebase, we can streamline and clean our history tree just like how we have described earlier.

Let’s start by undoing the previous merge.

$ git reset --hard HEAD~

在这里插入图片描述

Switch over to “issue3” branch and rebase onto the master branch.

$ git checkout issue3
Switched to branch 'issue3'

$ git rebase master
  First, rewinding head to replay your work on top of it...
  Applying: append description of the pull command
  Using index info to reconstruct a base tree...
  :13: new blank line at EOF.
  +
  warning: 1 line adds whitespace errors.
  Falling back to patching base and 3-way merge...
  Auto-merging myfile.txt
  CONFLICT (content): Merge conflict in myfile.txt
  Failed to merge in the changes.
  Patch failed at 0001 append description of the pull command
  
  When you have resolved this problem run "git rebase --continue".
  If you would prefer to skip this patch, instead run "git rebase --skip".
  To check out the original branch and stop rebasing run "git rebase --abort".

When a conflict occurs during the rebase, you will have to resolve it immediately in order to resume the rebase operation.

此时myfile.txt文件内容如下:

Git commands even a monkey can understand
add: Register a change in an index
<<<<<<< HEAD
commit: Save the status of an index
=======
pull: Obtain the content of a remote repository
>>>>>>> issue3

Once the conflict is resolved, you can resume rebase with the –continue option. If you wish to quit and rollback the rebase operation, you can do so by passing in the –abort option.

此时我们可以将文件修改为:

Git commands even a monkey can understand
add: Register a change in an index
commit: Save the status of an index
pull: Obtain the content of a remote repository

然后执行:

$ git add myfile.txt
$ git rebase --continue
Applying: append description of the pull command

在这里插入图片描述

With the “issue3” branch rebased onto “master”, we can now issue a fast-forward merge.

Switch over to the master branch and merge “issue3” with “master”.

$ git checkout master
Switched to branch 'master'

$ git merge issue3
Updating 8f7aa27..96a0ff0
Fast-forward
myfile.txt |    1 +
1 files changed, 1 insertions(+), 0 deletions(-)

The content of myfile.txt should now be identical to the one that we got from the previous merge. The revision history now should look like the following.

在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/568195.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

一篇文章全面了解光分路器、PLC分路器、拉锥分路器

光纤分路器 光纤分路器&#xff0c;又称为分光器&#xff0c;是将一根光纤信号按照既定的比例分解为两路或多路光信号输出&#xff0c;是接入FTTH方式的光无源器件。 例如&#xff0c;一个1x4光分路器就是将一根光纤中的光信号按照一定的比例分配给四根光纤。与WDM系统的波分复…

【Java入门】运算符

前言 &#x1f4d5;作者简介&#xff1a;热爱跑步的恒川&#xff0c;致力于C/C、Java、Python等多编程语言&#xff0c;热爱跑步&#xff0c;喜爱音乐的一位博主。 &#x1f4d7;本文收录于Java入门篇系列&#xff0c;该专栏主要讲解&#xff1a;什么是java、java的数据类型与变…

放大镜-第14届蓝桥杯省赛Scratch中级组真题第3题

[导读]&#xff1a;超平老师的《Scratch蓝桥杯真题解析100讲》已经全部完成&#xff0c;后续会不定期解读蓝桥杯真题&#xff0c;这是Scratch蓝桥杯真题解析第138讲。 放大镜&#xff0c;本题是2023年5月7日举行的第14届蓝桥杯省赛Scratch图形化编程中级组编程第3题&#xff0…

MAC环境下使用 xray 工具

这里不做过多介绍&#xff0c;下面链接讲的非常清楚&#xff0c;下面记录一下遇到的坑。 https://docs.xray.cool/#/tutorial/webscan_basic_crawler Mac环境下选择对应的工具 下载完以后&#xff0c;放入自己的目录下&#xff0c;打开终端查看版本信息 ./xray_darwin_amd64 v…

Jupyter程序安装和使用指南【操作示例】

Jupyter Notebook(简称Jupyter)是一个交互式编辑器&#xff0c;它支持运行40多种编程语言&#xff0c;便于创建和共享文档。Jupyter本质上是一个Web应用程序&#xff0c;与其他编辑器相比&#xff0c;它具有小巧、灵活、支持实时代码、方便图表展示等优点。下面分别为大家演示如…

在CTEX文档生成中使用WinEit编辑带有公式符号的中文文档应用举例

CTEX文档生成中使用WinEit编辑带有公式符号的中文文档应用举例 CTEX在编辑文档格式和排版时具有优秀的性能&#xff0c;可批量处理文档格式&#xff0c;该用格式时候也非常快捷。下面举例介绍CTEX文档生成中怎样使用WinEit编辑带有公式符号的中文文档。 1.需要的代码 .在WinEi…

IT入门深似海,入门到放弃你学废了嘛

我一直觉得IT行业 程序员行业。甚至觉得程序员人群 是一个特殊存在的群体。 入门到放弃&#xff0c;是真的&#xff0c;IT门槛高嘛。 其实吧&#xff0c;IT编程门槛&#xff0c;是有的&#xff0c;但是对于感兴趣的&#xff0c;想学习IT编程同学来说&#xff0c;也是一件容易事…

Few-Shot Knowledge Graph Completion

[1911.11298] Few-Shot Knowledge Graph Completion (arxiv.org) 目录 Background Model Encoding Heterogeneous Neighbors Aggregating Few-Shot Reference Set Matching Query and Reference Set Matching Query and Reference Set Background 以往的KGC认为每个关系…

【微信小程序】微信小程序集成高德卫星地图完成多边形绘制与截图保存

目录 功能需求 使用的技术点 注意点 实现步骤 代码 微信小程序-地图所在的wxml 微信小程序-地图所在的js 微信小程序-展示截图结果的wxml 微信小程序-展示截图结果的js H5-地图所在的html 完成效果 感谢阅读&#xff0c;欢迎讨论 功能需求 打开页面展示卫星地图&…

震惊!人工智能引发灰色经济,ChatGPT变身罪魁祸首!

人工智能技术的日益发展和普及&#xff0c;其呈现出无边界的开发空间&#xff0c;引领出无数的商业应用&#xff0c;越来越多的领域开始依赖这一技术&#xff0c;各种应用场景日益丰富&#xff0c;而其内在的巨大潜力也被不断开发。随之而来的则是&#xff0c;因为技术的滥用和…

13 张图,带你深入理解Synchronized,吊打所有大厂面试官

前言 分享一篇优质文章给你。 本文带读者们由浅入深理解Synchronized&#xff0c;让读者们也能与面试官疯狂对线&#xff0c;同时写出高性能的代码和架构。 在并发编程中Synchronized一直都是元老级的角色&#xff0c;Jdk 1.6以前大家都称呼它为重量级锁&#xff0c;相对于J…

freertos任务优先级分配

RQ 任务&#xff1a;IRQ 任务是指通过中断服务程序进行触发的任务&#xff0c;此类任务应该设置为所有任务里面优先 级最高的。高优先级后台任务&#xff1a;比如按键检测&#xff0c;触摸检测&#xff0c;USB 消息处理&#xff0c;串口消息处理等&#xff0c;都可以归为这一类…

Flink自定义函数之表值聚合函数(UDTAGG函数)

1.表值聚合函数概念 自定义表值聚合函数&#xff08;UDTAGG&#xff09;可以把一个表&#xff08;一行或者多行&#xff0c;每行有一列或者多列&#xff09;聚合成另一张表&#xff0c;结果中可以有多行多列。 理解&#xff1a;假设有一个饮料的表&#xff0c;这个表有 3 列&a…

MF矩阵分解——SVD、LFM、RSVD、SVD++

文章目录 1 矩阵分解MF针对问题2 解决思路2.1 引例2.2 实际应用中 3 MF的几种方式3.1 特征值分解特征值、特征向量特征值分解缺点 3.2 奇异值分解(SVD)示例&#xff1a;基本定理计算步骤缺点 3.3 Basic SVD&#xff08;LFM、Funk SVD&#xff09;3.4 RSVD进一步优化 3.5 SVD 4 …

mybatis高频面试题

什么是mybatis mybatis框架是一个开源的数据持久性层框架它的内部封装了通过JDBC访问数据库的操作&#xff0c;支持普通的SQL查询、存储过程和高级映射&#xff0c;几乎消除了所有的JDBC代码和参数的手工设置以及结果集的检索。MyBatis作为持久层框架&#xff0c;其主要思想是…

【完美解决】mysql启动不了:本地计算机上的MySQL服务启动后停止

本文基于mysql8.0&#xff0c;5.7也可以参考 navicat 突然莫名其妙连不上mysql 查看服务&#xff0c;也启动不了&#xff0c;手动启动出现错误&#xff1a; 本地计算机上的MySQL服务启动后停止。某些服务在未由其他服务或程序使用时将自动停止 20230525更新&#xff01; 先…

Jmeter组件:Random CSV Data Set Config(随机读取文件数据)

一、Jmeter组件&#xff1a;Random CSV Data Set Config(随机读取文件数据) 功能&#xff1a;该组件可以随机读取CSV文件中的每一行的数据 二、下载插件&#xff1a;(jmeter-plugins-random-csv-data-set-xx.jar),并放到lib/ext目录下&#xff0c;重启jmeter 也可以在Jmeter…

【软考-中级】系统集成项目管理工程师【12 沟通和关系人】

持续更新。。。。。。。。。。。。。。。 【第十二章】沟通和关系人 2 分 12.1 沟通的基本概念12.1.1沟通的定义11.1.2 沟通的方式 12.2制订沟通管理计划12.2.1制订沟通管理计划的输入12.2.2制订沟通管理计划的工具 12.3 管理沟通12.3.1管理沟通输入12.3.2管理沟通的工具12.3.3…

IMX6ULL裸机篇之RTC实验原理

一. RTC实时时钟简介 实时时钟是很常用的一个外设&#xff0c;通过实时时钟我们就可以知道年、月、日和时间等信息。 因此在需要记录时间的场合&#xff0c;可以使用专用的实时时钟芯片来完成此功能。 但是&#xff0c;现在大多数的 MCU 或者 MPU 内部就已经自带了实时时钟外…

JVM进程缓存+Lua语法初始+缓存同步策略

传统的缓存策略是一般请求到达Tomact之后&#xff0c;先进行查询Redis&#xff0c;如果未命中则进行查询数据库&#xff0c;是存在着下面的问题的: 1)请求要经过Tomact进行处理&#xff0c;Tomact的性能成为整个系统的瓶颈&#xff1b; 2)当Redis缓存失效的时候&#xff0c;会对…