git分支

git系列文章目录

第一章 什么分支和git分支


前言

工作中不可避免的会遇到分工合作,如果某些功能交给新手开发,难免会出问题,这样的话为了不影响工作进度,就需要分工合作,等某部分完全稳定的时候再添加到主功能中,这样才是比较稳妥的方法

一、分支是什么?

分支是git分工完成任务的一种团队协作方式,每一个功能都创建一个分支的最主要作用是为了避免相关功能污染主分支。

设想一下啊,如果某个软件需要多个测试功能不能稳定运行,那么也没有必要和主分支合并,这样就可以只合并稳定的功能然后迭代

在这里插入图片描述

在这里插入图片描述

二、使用步骤

1.引入新文件(工作中实现某部分新的功能这里只添加文本文件演示)

如下(示例):先新建一个test4.txt



Apple@DESKTOP-ECLNIU2 MINGW64 ~/Desktop/git basic operation/git-basic (master)
$ ll
total 25
-rw-r--r-- 1 Apple 197121  110 Jan 22 16:38 GitBasic.add
-rw-r--r-- 1 Apple 197121 9719 Jan 22 16:38 LICENSE
-rw-r--r-- 1 Apple 197121   45 Jan 22 17:17 OtherUser.add
-rw-r--r-- 1 Apple 197121  863 Jan 22 16:38 README.en.md
-rw-r--r-- 1 Apple 197121 1001 Jan 22 16:38 README.md
-rw-r--r-- 1 Apple 197121    0 Jan 22 17:23 add1.txt
-rw-r--r-- 1 Apple 197121    0 Jan 22 17:23 add2.txt
-rw-r--r-- 1 Apple 197121    0 Jan 22 17:23 add3.txt
-rw-r--r-- 1 Apple 197121    0 Jan 22 17:23 add4.txt
-rw-r--r-- 1 Apple 197121   61 Jan 22 17:55 test.txt
-rw-r--r-- 1 Apple 197121    0 Jan 22 18:52 test2.txt
-rw-r--r-- 1 Apple 197121   22 Jan 22 19:28 test3.txt
-rw-r--r-- 1 Apple 197121   17 Jan 22 19:11 test4.txt

Apple@DESKTOP-ECLNIU2 MINGW64 ~/Desktop/git basic operation/git-basic (master)
$ cat test4.txt
aaabbb
cccc
ddd


2.添加到本地仓库

代码如下(示例):

Apple@DESKTOP-ECLNIU2 MINGW64 ~/Desktop/git basic operation/git-basic (master)
$ git add test4.txt

Apple@DESKTOP-ECLNIU2 MINGW64 ~/Desktop/git basic operation/git-basic (master)
$ git commit -m "创建添加了test4.txt,用来演示分支" test4.txt
[master c2b6b4c] 创建添加了test4.txt,用来演示分支
 1 file changed, 2 insertions(+), 1 deletion(-)

Apple@DESKTOP-ECLNIU2 MINGW64 ~/Desktop/git basic operation/git-basic (master)
$ git status
On branch master
Your branch is ahead of 'origin/master' by 14 commits.
  (use "git push" to publish your local commits)

nothing to commit, working tree clean

Apple@DESKTOP-ECLNIU2 MINGW64 ~/Desktop/git basic operation/git-basic (master)
$

该处已经将test4.txt提交到本地仓库。

2.查看本地所有分支

目前只有一条主分支master使用命令查看

Apple@DESKTOP-ECLNIU2 MINGW64 ~/Desktop/git basic operation/git-basic (master)
$ git branch -v
* master c2b6b4c [ahead 14] 创建添加了test4.txt,用来演示分支

Apple@DESKTOP-ECLNIU2 MINGW64 ~/Desktop/git basic operation/git-basic (master)
$ git reflog
c2b6b4c (HEAD -> master) HEAD@{0}: commit: 创建添加了test4.txt,用来演示分支
0aa8a43 HEAD@{1}: commit: 更改test3演示了暂存区和本地库比较差异,结束后提交到本地库
a163bda HEAD@{2}: commit: 增加了一行空格和一行eee
e61828e HEAD@{3}: commit: 添加了一个test4.txt,并提交到本地库
8c42001 HEAD@{4}: commit: 新建一个test3.txt,并添加本地库
064ffb3 HEAD@{5}: reset: moving to 064ffb3
064ffb3 HEAD@{6}: reset: moving to 064ffb3
b5dfe8a HEAD@{7}: reset: moving to HEAD
b5dfe8a HEAD@{8}: commit: 删除了test2.txt,并同步到本地库
064ffb3 HEAD@{9}: commit: 新建一个test2.txt并提交到本地库
aa79b89 HEAD@{10}: reset: moving to aa79b89
2634270 HEAD@{11}: reset: moving to 2634270
db2d825 HEAD@{12}: reset: moving to db2d825
aa79b89 HEAD@{13}: commit: 又增加了一行d
db2d825 HEAD@{14}: commit: 又增加了一行c
2634270 HEAD@{15}: commit: 又增加了一行b
9f010b9 HEAD@{16}: commit: 在test.txt中增加了一行字符串a
81b1df6 HEAD@{17}: commit: 增加了一个test.txt
1c75a36 HEAD@{18}: commit: 手动添加4个测试文件add1~4用于演示_reset
1215585 HEAD@{19}: commit: 修改OtherUser.add
6b65ae8 HEAD@{20}: commit: 使用另外一个用户clone仓库后添加文件并commit
32a5af7 (origin/master, origin/HEAD) HEAD@{21}: clone: from https://gitee.com/gitee309307194/git-basic.git

Apple@DESKTOP-ECLNIU2 MINGW64 ~/Desktop/git basic operation/git-basic (master)
$


3.创建本地分支

创建一个新的分支branch01


Apple@DESKTOP-ECLNIU2 MINGW64 ~/Desktop/git basic operation/git-basic (master)
$ git branch branch01

Apple@DESKTOP-ECLNIU2 MINGW64 ~/Desktop/git basic operation/git-basic (master)
$ git branch -v
  branch01 c2b6b4c 创建添加了test4.txt,用来演示分支
* master   c2b6b4c [ahead 14] 创建添加了test4.txt,用来演示分支

Apple@DESKTOP-ECLNIU2 MINGW64 ~/Desktop/git basic operation/git-basic (master)
$


注意:当前所在的分支前面有*,说明当前所在的分支是master分支

3.切换本地分支

Apple@DESKTOP-ECLNIU2 MINGW64 ~/Desktop/git basic operation/git-basic (master)
$ git checkout branch01
Switched to branch 'branch01'

Apple@DESKTOP-ECLNIU2 MINGW64 ~/Desktop/git basic operation/git-basic (branch01)
$ git branch -v
* branch01 c2b6b4c 创建添加了test4.txt,用来演示分支
  master   c2b6b4c [ahead 14] 创建添加了test4.txt,用来演示分支

Apple@DESKTOP-ECLNIU2 MINGW64 ~/Desktop/git basic operation/git-basic (branch01)
$

此时branch01前面有*,说明已经切换到branch01分支了。而且两个分支的版本号是一样的。工作中如果有新人的话,就可以让其在该分支后面完成相应的功能了


总结

例如:以上就是今天要讲的内容,本文仅仅简单介绍了简单的分支查看,切换,和创建分支
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值