Git quick start (3)__ branch creation, switching and merging

1. Understanding branches

For ease of understanding, you can roughly think of a branch as a copy of the code.

If we develop multiple functions on one code at the same time. Some bugs need to be modified. During the collaboration process of team members, there will inevitably be mutual influence.

If a colleague submits a wrong code, it may cause other colleagues who have updated the code to fail to compile normally. Influence each other.

A better practice is that when we develop a new function or modify a bug, we can pull out a branch. Operations on this branch will not affect the work of other colleagues. After your work is completed, you can merge the code back into the latest code.

2. The usefulness of common branches master, develop, release, hotfix, feature

1) The main branch master is the branch of the external release version

When we initially commit to git, a master branch is automatically generated

The master branch generally holds the released version.

2) develop branch development branch

In order to maintain the stability of the code on the main branch (the main branch usually holds the version released to users), the development work is not carried out on the main branch, but on the develop branch.

When develop completes the development task, Black Ice goes to the master branch

3) feature branch

All team members work on develop. In order to maintain the stability of the development branch, a branch should be pulled out when each function is developed, which is based on the develop branch and merged into develop when the development is completed.

During the development process, when a certain function is to be developed, a function branch will be pulled

4) hotfix branch

When modifying the bug, you can also pull the branch from the development branch (develop), modify it on the branch, and then merge the code into the development branch (develop) after completion

3.git branch command

git branch -v view branch

git branch branch name create branch

git checkout branch name switch branch

git checkout -b 分支名 创建并切换到分支(等价于 git branch 分支名+git checkout 分支名)

git merge 分支名 将指定的分支合并到当前分支

git branch -d 分支名 删除指定名称的分支(分支被合并后才允许删除,如果要删除,需要使用-D 强制删除)

4. git分支实操

1) 准备工作

为了方便进行后续的代码合并以及冲突的测试,请大家克隆你的码云上的最新的cmo_week04 到本地文件夹“week502”。

  1. 请大家使用自己的gitee账号登录gitee.com

  1. fork 仓库https://gitee.com/caohongxing7604/cmo_week04 到你的gitee上

  1. 本地计算机上创建一个文件夹week502

  1. 在week502de父目录中打开git命令窗口

  1. 执行 git clone ,记得把中间的地址改为你的仓库地址奥

git clone https://gitee.com/caohx666/cmo_week04 week502/

2) git branch 查看分支

进入到刚才创建的week502目录,右键点击 git Bash here.. 打开git命令窗口,

输入:git branch -v

这个命令练习起来,后面我们要反复使用该命令奥!

3)git branch 分支名 创建分支

下面我们的实验创建develop分支,然后再develop上再依次创建2个分支:feature_add分支 和feature_ sub 分支,分别用来开发add功能和sub功能。

git branch develop 创建develop分支

git checkout develop 切换到develop分支

git branch feature_add 创建一个名为add的分支,注意,此时仍在原来的分支上

git branch feature_sub 创建另一个名为sub的分支,用来开发 sub 功能

git branch 查看分支情况

4)git checkout 分支名 切换分支

上一步我们已经创建了分支。

在这一步,我们要模拟两位开发切换到他们的分支,提交代码到各自的分支上。

首先模拟开发1切换到分支feature_add上提交代码,并提交到本地仓库

  1. 下面我们切换到feature_add分支上,并添加一个 add.txt 的文件,

  1. feature_add分支上,并在001.txt的文件的第六行之后删除掉,末尾添加add函数

int add(int x , int y)

{

return x+y;

}

  1. 提交以上修改到本底裤,日志为:xxx 分支git branch feature_add 上修改 新增了add函数

然后模拟开发2切换到分支feature_sub上提交代码,并提交到本地仓库

  1. 下面我们切换到feature_sub分支上,修改001.txt,将5行之后的内容删除,加上sub函数

int sub(int x , int y)

{

return x-y;

}

  1. .提交分支 feature_sub 上修改,日志为:xxx 分支git branch feature_sub 上修改 新增了sub函数 xxx为学生姓名

需要注意的是,当前分支如果有未提交到暂存区的修改,切换分支会有错误提醒,提示你提交修改或者保存文件

我们此时,你可以将修改commit到本地仓库,

如果你觉得还没有修改好,那你也可以使用git stash 将其隐藏,以后再切换回分支时,再将其恢复

5)git merge 分支名 合并分支

合并分支时,需要先切换到目标分支,然后再合并。

为了减少后期冲突,合并前,建议git pull 从服务器上拉取最新代码。

(1) 将分支 git branch feature_add 合并到主干

我们切换到develop分支上,将feature_add 分支合并到develop

(2)将分支git branch feature_sub 也合并到主干

我们切换到develop分支上,将feature_add 分支合并到develop

合并过程中,由于feature_add 和feature_sub分支修改了相同行,提交时会提示冲突,需要手动解决冲突。

如果你安装了 TortoiseGit。则可以通过图形化工具来解决冲突,如果没有,直接查看001.txt也可以

手动解决冲突后,再次 添加到暂存区后,提交到本地库

6)将develop、feature_add feature_sub分支提交到远程仓库

如果愿意,大家也可以将分支推送到远程分支

6)git branch -d 分支名 删除分支

删除分支前,需要切换到其他分支

7)查看日志,截屏

git log --oneline --graph --decorate --all 是常用的日志查看命令

截屏日志结果

4.gitee仓库的删除

在确认窗口中,输入确认信息后,点击【确认删除】,则可以成功删除仓库

在验证码确认窗口上输入账户密码后,点击【验证】

5.git 冲突解决

git pull --allow-unrelated-histories

Guess you like

Origin blog.csdn.net/caohongxing/article/details/129689445