当前位置:   article > 正文

Git之Feature分支

feature分支

为什么要用到分支

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

实例分析

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

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

如果产生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'
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

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

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
  • 1
  • 2
  • 3
  • 4

此时如果有出现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
  • 1
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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

切回master,准备合并

$ git switch master
  • 1

一切顺利的话,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'.
  • 1
  • 2
  • 3
  • 4

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

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

终于删除成功!

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/533121
推荐阅读
  

闽ICP备14008679号