Git分支结构

目录

1. 线性分支结构

2. 分叉与合并结构

3. 分支与标签的关系

4. 并行开发与分支管理策略

测试(本机系统为Rocky_linux9.4)

合并失败解决

删除分支

 删除本地分支

删除远程分支


Git 中的分支结构是版本控制中非常重要的概念之一,它描述了项目中不同提交的组织方式和分支之间的关系。以下是Git 分支结构及其解释:

1. 线性分支结构

  • 最简单的分支结构是线性的,每次提交都是在前一次提交的基础上进行的。这种结构没有分叉或合并,所有的提交都在同一个主线上。
A --- B --- C --- D  (master)
  • 上面的示例中,ABCD 表示不同的提交,master 分支顺序地指向每个新的提交。

2. 分叉与合并结构

  • 在实际项目中,通常会存在多个并行开发的分支,这些分支可以同时在不同的特性或修复上工作,然后将它们合并回主分支或其他分支。
        /--- E --- G (feature1)
A --- B --- C --- D
        \--- F --- H (feature2)
  •  在上面的示例中,feature1feature2 是从 B 提交分叉出来的分支。EF 是各自分支上的提交,GH 是将特性分支合并回主分支后的提交。

3. 分支与标签的关系

  • 标签(Tag)通常用于标记重要的版本发布点或里程碑,它们可以指向任意的提交,不一定是分支的头部。
A --- B --- C --- D (master, v1.0)
                   \
                    E --- F (v2.0)
  • 在这个示例中,v1.0 标签指向提交 D,表示 v1.0 版本的发布点。而 v2.0 标签指向提交 F,表示 v2.0 版本的发布点。

4. 并行开发与分支管理策略

       在团队协作中,良好的分支管理策略可以有效地管理并行开发和版本控制。常见的策略包括使用主分支(如 master)作为稳定版本的发布线,使用特性分支来开发新功能或修复 bug,并通过合并操作将它们整合回主分支。

测试(本机系统为Rocky_linux9.4)

分支切换

[root@tty02 tty]# git branch newrain    #创建一个分支
[root@tty02 tty]# git branch
* (HEAD detached at ugo)
  master
  newrain
[root@tty02 tty]# git checkout newrain  #切换到这个分支
Switched to branch 'newrain'
[root@tty02 tty]# git branch
  master
* newrain

 在newrain分支进行修改

[root@tty02 tty]# ll
total 0
-rw-r--r-- 1 root root 0 Jul  9 22:37 tty
[root@tty02 tty]# echo "1901" >> tty
[root@tty02 tty]# cat tty
1901
[root@tty02 tty]# git add .
[root@tty02 tty]# git commit -m '1901']
[newrain 96638c2] 1901]
 1 file changed, 1 insertion(+)
[root@tty02 tty]# git status
On branch newrain
nothing to commit, working tree clean

#此时位于分支 newrain 无文件要提交,干净的工作区

回到master分支

[root@tty02 tty]# git checkout master
Switched to branch 'master'
Your branch is based on 'origin/master', but the upstream is gone.
  (use "git branch --unset-upstream" to fixup)
[root@tty02 tty]# git branch
* master
  newrain
[root@tty02 tty]# ll
total 0
-rw-r--r-- 1 root root 0 Jul  9 23:39 tty
[root@tty02 tty]# cat tty                       #此时在master分支上看不到内容
[root@tty02 tty]# git log  -1
commit d2470f142c7721ebbff8a3b8f5f7752ecbae67c8 (HEAD -> master, tag: v2.0, tag: v1.0, tag: ugo)
Author: Your Name <you@example.com>
Date:   Tue Jul 9 22:35:17 2024 +0800

    commit

合并代码

[root@tty02 tty]# git merge newrain    #合并newrain到当前master分支
Updating d2470f1..96638c2
Fast-forward
 tty | 1 +
 1 file changed, 1 insertion(+)

[root@tty02 tty]# git status
On branch master
Your branch is based on 'origin/master', but the upstream is gone.
  (use "git branch --unset-upstream" to fixup)

nothing to commit, working tree clean

[root@tty02 tty]# cat tty
1901

合并后即可看到内容

合并失败解决

模拟冲突,在文件的同一行做不同修改

在master分支进行修改 

[root@tty02 tty]# cat tty
1901
[root@tty02 tty]# echo '1901-git' > tty
[root@tty02 tty]# git add .
[root@tty02 tty]# git commit -m 'newrain 1901-git'
[master d343709] newrain 1901-git
 1 file changed, 1 insertion(+), 1 deletion(-)

 切换到newrain分支

[root@tty02 tty]# git checkout newrain
Switched to branch 'newrain'

[root@tty02 tty]# git branch
  master
* newrain

[root@tty02 tty]# cat tty
1901

[root@tty02 tty]# echo 'newrain' >> tty
[root@tty02 tty]# git add .
[root@tty02 tty]# git commit -m '1901-git-check'
[newrain d32a6f0] 1901-git-check
 1 file changed, 1 insertion(+)

回到master分区,进行合并,出现冲突

[root@tty02 tty]# git checkout master
Switched to branch 'master'
Your branch is based on 'origin/master', but the upstream is gone.
  (use "git branch --unset-upstream" to fixup)
[root@tty02 tty]# git branch
* master
  newrain


[root@tty02 tty]# git merge newrain             #此时合并遇到了冲突
Auto-merging tty
CONFLICT (content): Merge conflict in tty
Automatic merge failed; fix conflicts and then commit the result.

解决冲突

[root@tty02 tty]# cat tty   #查看该文件
<<<<<<< HEAD
1901-git
=======
1901
newrain
>>>>>>> newrain
解释:
<<<<<<< HEAD
// 当前分支的修改内容
=======
// 待合并分支的修改内容
>>>>>>> newrain

 编辑该冲突文件,手动删除不需要的部分即可

[root@tty02 tty]# vim tty    #删除后见下图,下图就是我要保留的内容

[root@tty02 tty]# git add .
[root@tty02 tty]# git commit -m "解决合并冲突"
[master 47f3b39] 解决合并冲突

 此时就已经解决该冲突。

删除分支

因为之前已经合并了newrain分支,所以现在看到它在列表中。 在这个列表中分支名字前没有 * 号的分支通常可以使用 git branch -d 删除掉;刚刚已经将它们的工作整合到了另一个分支,所以并不会失去任何东西。查看所有包含未合并工作的分支,可以运行 git branch --no-merged

如果真的想要删除分支并丢掉那些工作,如同帮助信息里所指出的,可以使用 -D 选项强制删除它。

 删除本地分支

[root@tty02 tty]# git branch   #查看分支
* master
  newrain

[root@tty02 tty]# git branch -d newrain    #删除这个分支
Deleted branch newrain (was d32a6f0).

[root@tty02 tty]# git branch               #此时就没有了
* master  

删除远程分支

如果需要删除远程仓库中的分支,可以使用 git push 命令和 --delete 选项:

git push origin --delete <branch-name>

这样就完成了删除分支的操作。在执行任何删除操作之前,确保理解并确认删除的分支不再需要,并且对删除的后果有清晰的了解。

  • 26
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ZZDICT

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值