Git之Feature分支

为什么要用到分支

  软件开发中,总有无穷无尽的新功能要不断添加进来。
  在添加一个新功能的时候,你肯定不希望因为一些实验性质的代码,把主分支搞乱了,所以,每添加一个新功能,最好新建一个feature分支,在上面开发,完成后,合并,最后,删除该feature分支。

实例分析

  假如现在你接到了一个新任务,开发代号为feature-visual,该功能用于编写可视化的接口。
于是准备开发

$ git switch -c feature-visual
fatal: not a git repository (or any of the parent directories): .git

如果产生fatal: not a git repository (or any of the parent directories): .git的报错,报错提示:没有.git这个目录
解决办法:将git进行初始化,在进行创建分支

administrator@BF-201802061547 MINGW64 ~
$ git init
Initialized empty Git repository in C:/Users/Administrator/.git/

administrator@BF-201802061547 MINGW64 ~ (master)
$ git switch -c feature-visual
Switched to a new branch 'feature-visual'

几分钟后,开发完毕:
将我们的新分支添加到暂存区

administrator@BF-201802061547 MINGW64 ~ (feature-visual)
$ git add visual.py
warning: LF will be replaced by CRLF in visual.py.
The file will have its original line endings in your working directory

此时如果有出现warning: LF will be replaced by CRLF in visual.py.The file will have its original line endings in your working directory的提示,这可能是你的项目使用了GitHub的开源项目,这个开源项目上传的环境(Linux)和你本次上传GitHub仓库的环境(Windows)不同,例如:本机上传的环境是win10 ,而在你本机项目中引用的GitHub项目上传环境是Linux,windows中的换行符为 CRLF,而在Linux下的换行符为LF
解决办法:可以选择忽略,也可以敲下面的命令

git config --global core.autocrlf false
administrator@BF-201802061547 MINGW64 ~ (feature-visual)
$ git status
On branch feature-visual

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
        new file:   visual.py
administrator@BF-201802061547 MINGW64 ~ (feature-visual)
$ git commit -m "add feature vulcan"
[feature-visual (root-commit) 24d1a7a] add feature vulcan
 1 file changed, 2 insertions(+)
 create mode 100644 visual.py

切回master,准备合并

$ git switch master

一切顺利的话,feature分支和bug分支是类似的,合并,然后删除。
但是!
就在这时,接到上级的命令,因经费不足,新功能必须取消!
虽然白干了,但是这个包含机密资料的分支还是必须就地销毁:

administrator@BF-201802061547 MINGW64 ~ (master)
$ git branch -d feature-visval
error: The branch 'feature-visval' is not fully merged.
If you are sure you want to delete it, run 'git branch -D feature-visval'.

销毁失败。Git友情提醒,feature-visual分支还没有被合并,如果删除,将丢失掉修改,如果要强行删除,需要使用大写的-D参数。。
现在我们强行删除:

administrator@BF-201802061547 MINGW64 ~ (master)
$ git branch -D feature-visval
Deleted branch feature-visval (was 9e06f3c).

终于删除成功!

  • 7
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值