zoukankan      html  css  js  c++  java
  • Git 分支的一些特殊的使用方式:Bug分支/feature分支/储存现场/

    参考链接:https://www.liaoxuefeng.com/wiki/896043488029600/900388704535136

    一般都与dev分支进行合并

    Bug分支

    Bug分支也是一个分支,他甚至和前面创建的分支没有区别,只是在Git中,分支是如此的强大,以至于在修复Bug的时候,所以,每个bug都可以通过一个新的临时分支来修复,修复后,合并分支,然后将临时分支删除。所以

      首先确定要在哪个分支上修复bug,假定需要在master分支上修复,就从master创建临时分支:

    $ git checkout master
    Switched to branch 'master'
    Your branch is ahead of 'origin/master' by 6 commits.
      (use "git push" to publish your local commits)
    
    $ git checkout -b issue-101
    Switched to a new branch 'issue-101'
    

      现在修复bug,需要把“Git is free software ...”改为“Git is a free software ...”,然后提交:

    $ git add readme.txt 
    $ git commit -m "fix bug 101"
    [issue-101 4c805e2] fix bug 101
     1 file changed, 1 insertion(+), 1 deletion(-)
    

      修复完成后,切换到master分支,并完成合并,最后删除issue-101分支

    保存现场

    Bug分支在使用起来和之前的分支并没有什么不同,依旧是众所周知的创建/修改工作/添加/提交/返回master/合并/删除bug分支.这并没有什么好讲的.

    特殊的是一种在我们准备创建Bug分支的时候可能会遇到的一种意外情况,就是我们正在dev分支上的工作还没有达到能够提交的要求.即我们的工作区还有文件(使用git status 来查看是否有改动还没有提交).而在我们创建新的分支的时候,操作的对象就是当前工作区中的内容.

    $ git status
    On branch dev
    Changes to be committed:
      (use "git reset HEAD <file>..." to unstage)
    
    	new file:   hello.py
    
    Changes not staged for commit:
      (use "git add <file>..." to update what will be committed)
      (use "git checkout -- <file>..." to discard changes in working directory)
    
    	modified:   readme.txt
    

      幸好,Git还提供了一个stash功能,可以把当前工作现场“储藏”起来,等以后恢复现场后继续工作:

    $ git stash#这就好了
    Saved working directory and index state WIP on dev: f52c633 add merge
    

      现在,用git status查看工作区,就是干净的(除非有没有被Git管理的文件),因此可以放心地创建分支来修复bug。

      当bug修复好了后.是时候接着回到dev分支干活了!

    $ git checkout dev
    Switched to branch 'dev'
    
    $ git status
    On branch dev
    nothing to commit, working tree clean
    

      工作区是干净的,刚才的工作现场存到哪去了?用git stash list命令看看:

    $ git stash list
    stash@{0}: WIP on dev: f52c633 add merge
    

      

    工作现场还在,Git把stash内容存在某个地方了,但是需要恢复一下,有两个办法:

    一是用git stash apply恢复,但是恢复后,stash内容并不删除,你需要用git stash drop来删除;

    另一种方式是用git stash pop,恢复的同时把stash内容也删了:

    $ git stash pop
    On branch dev
    Changes to be committed:
      (use "git reset HEAD <file>..." to unstage)
    
    	new file:   hello.py
    
    Changes not staged for commit:
      (use "git add <file>..." to update what will be committed)
      (use "git checkout -- <file>..." to discard changes in working directory)
    
    	modified:   readme.txt
    
    Dropped refs/stash@{0} (5d677e2ee266f39ea296182fb2354265b91b3b2a)
    

      再用git stash list查看,就看不到任何stash内容了:

      你可以多次stash,恢复的时候,先用git stash list查看,然后恢复指定的stash,用命令:

    $ git stash apply stash@{0}
    

      

    小结

    修复bug时,我们会通过创建新的bug分支进行修复,然后合并,最后删除;

    当手头工作没有完成时,先把工作现场git stash一下,然后去修复bug,修复后,再git stash pop,回到工作现场。

    Feature分支

    参考链接:https://www.liaoxuefeng.com/wiki/896043488029600/900394246995648

    这也是分支的另一种叫法

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

    有时也可能会遇到一些额外的情况,比如你在新建的Feature分支中已经把新功能写好了,就在你切回了dev分支,正准备合并的时候,突然接到要求说这个功能取消了,即使写好了,虽然白干了,但是这个包含机密资料的分支还是必须就地销毁:

    $ git branch -d feature-vulcan
    error: The branch 'feature-vulcan' is not fully merged.
    If you are sure you want to delete it, run 'git branch -D feature-vulcan'.
    

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

      现在我们强行删除:  

    $ git branch -D feature-vulcan
    Deleted branch feature-vulcan (was 287773e).
    

      

    小结

    开发一个新feature,最好新建一个分支;

    如果要丢弃一个没有被合并过的分支,可以通过git branch -D <name>强行删除。

  • 相关阅读:
    numpy中的随机数模块
    windows下用pycharm安装tensorflow简易教程
    Tensor是神马?为什么还会Flow?
    TypeError: 'NoneType' object is not subscriptable
    pycharm 运行错误信息显示乱码
    pycharm terminal 'import' 不是内部或外部命令,也不是可运行的程序
    pycharm 出现 "PEP:8 expected 2 blank lines ,found 0"
    TensorFlow升级1.4:Cannot remove entries from nonexistent file libsite-pack
    win10 python3.5 自动补全设置
    python pip NameError:name 'pip' is not defined”
  • 原文地址:https://www.cnblogs.com/Gaoqiking/p/11116867.html
Copyright © 2011-2022 走看看