zoukankan      html  css  js  c++  java
  • Git学习笔记(6)——Bug和Feature分支

    本文主要记录了通过Git来调试Bug和添加新的功能,也就是Bug分支和Feature分支,以及分支的推送。


    Bug分支

    通过Git,我们可以为每个Bug建立一个分支,Bug修复后,合并分支,然后将临时分支删除。

    当有Bug的时候,想创建一个分支bug-101来修复它,如果,当前正在dev上进行的工作还没有完成,不能提交,而且,我们必须马上解决bug,这时,我们借助Git提供的stash功能,可以把当前工作现场“储藏”起来,等以后恢复现场后继续工作。

    ubuntu@myUbuntu:~/joe/learngit$ git branch
      dev
    * master
    ubuntu@myUbuntu:~/joe/learngit$ 
    git checkout dev
        //我们正在dev上面修改文件
    切换到分支 'dev'
    ubuntu@myUbuntu:~/joe/learngit$ ls
    abc.c  readme.txt
    ubuntu@myUbuntu:~/joe/learngit$ vi abc.c 
    ubuntu@myUbuntu:~/joe/learngit$ git status    
    位于分支 dev
    尚未暂存以备提交的变更:
      (使用 "git add <file>..." 更新要提交的内容)
      (使用 "git checkout -- <file>..." 丢弃工作区的改动)
    
        修改:     abc.c
    
    修改尚未加入提交(使用 "git add" 和/或 "git commit -a")
    ubuntu@myUbuntu:~/joe/learngit$ 
    git stash
        //要修改bug了,我们先存储当前的分支
    Saved working directory and index state WIP on dev: b961f85 dev
    HEAD 现在位于 b961f85 dev
    ubuntu@myUbuntu:~/joe/learngit$ 
    git checkout master
        //回到主分支建立bug-101分支
    切换到分支 'master'
    您的分支与上游分支 'origin/master' 一致。
    ubuntu@myUbuntu:~/joe/learngit$ 
    git checkout -b bug-101
    
    切换到一个新分支 'bug-101'
    ubuntu@myUbuntu:~/joe/learngit$ 
    vi abc.c
         //修改bug,然后提交完工
    ubuntu@myUbuntu:~/joe/learngit$ cat abc.c 
    bug is ok
    ubuntu@myUbuntu:~/joe/learngit$ git add abc.c 
    ubuntu@myUbuntu:~/joe/learngit$
    git commit -m "fix bug-101"
    
    [bug-101 5ad5f95] fix bug-101
     1 file changed, 1 insertion(+), 1 deletion(-)
    ubuntu@myUbuntu:~/joe/learngit$ 
    git checkout master
        //回到主分支
    切换到分支 'master'
    您的分支与上游分支 'origin/master' 一致。
    //以非快速模式合并分支
    ubuntu@myUbuntu:~/joe/learngit$
    git merge --no-ff -m "merge buf-101" bug-101
    
    Merge made by the 'recursive' strategy.
     abc.c | 2 +-
     1 file changed, 1 insertion(+), 1 deletion(-)
    ubuntu@myUbuntu:~/joe/learngit$ 
    git branch -d bug-101
        //删除bug分支
    已删除分支 bug-101(曾为 5ad5f95)。
    ubuntu@myUbuntu:~/joe/learngit$ 
    git checkout dev
        //回到dev分支上面
    切换到分支 'dev'
    ubuntu@myUbuntu:~/joe/learngit$ git status
    位于分支 dev
    无文件要提交,干净的工作区
    ubuntu@myUbuntu:~/joe/learngit$ 
    git stash list
        //查看存储区,发现有一条刚才的存储
    stash@{0}: WIP on dev: b961f85 dev
    ubuntu@myUbuntu:~/joe/learngit$ 
    git stash pop
        //弹出存储区
    位于分支 dev
    尚未暂存以备提交的变更:
      (使用 "git add <file>..." 更新要提交的内容)
      (使用 "git checkout -- <file>..." 丢弃工作区的改动)
    
        修改:     abc.c
    
    修改尚未加入提交(使用 "git add" 和/或 "git commit -a"丢弃了 refs
    /stash@{0
    } (264672afc6d36af005a5a27e8c165ad89216eb2d)
    ubuntu@myUbuntu:~/joe/learngit$ 
    git stash list
        //存储区为空。
    
    //工作现场还在,Git把stash内容存在某个地方了,但是需要恢复一下,有两个办法:
    
    //(1)git stash apply恢复,但是恢复后,stash内容并不删除,你需要用git stash drop来删除;
    
    //(2)git stash pop,恢复的同时把stash内容也删了:

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

    $ git stash apply stash@{0}

    Feature分支

    ubuntu@myUbuntu:~/joe/learngit$ git branch
      dev
    * master
    //接到命令添加新功能,创建功能分支
    ubuntu@myUbuntu:~/joe/learngit$ 
    git checkout -b feature-01
        
    切换到一个新分支 'feature-01'
    ubuntu@myUbuntu:~/joe/learngit$ vi abc.c 
    ubuntu@myUbuntu:~/joe/learngit$ cat abc.c 
    I am a new feature.
    ubuntu@myUbuntu:~/joe/learngit$ git add abc.c 
    ubuntu@myUbuntu:~/joe/learngit$ git status
    位于分支 feature-01
    要提交的变更:
      (使用 "git reset HEAD <file>..." 撤出暂存区)
    
        修改:     abc.c
    
    ubuntu@myUbuntu:~/joe/learngit$ 
    git commit -m "add feature-01"
       //完工以后,提交
    [feature-01 683e3bb] add feature-01
     1 file changed, 1 insertion(+), 1 deletion(-)
    ubuntu@myUbuntu:~/joe/learngit$ 
    git checkout dev
        //回到其他分支,准备合并后删除删除功能分支
    切换到分支 'dev'
    ubuntu@myUbuntu:~/joe/learngit$ git branch
    * dev
      feature-01
      master
    //接到命令,新功能取消,(不合并功能分支)删除功能分支
    ubuntu@myUbuntu:~/joe/learngit$ 
    git branch -d feature-01
    //提示,没有合并,不能删除,如需强制删除,使用参数D    
    error: 分支 'feature-01' 没有完全合并。    
    如果您确认要删除它,执行 'git branch -D feature-01'。
    ubuntu@myUbuntu:~/joe/learngit$ 
    git branch -D feature-01
        //强制删除分支
    已删除分支 feature-01(曾为 683e3bb)。
    ubuntu@myUbuntu:~/joe/learngit$ git branch
    * dev
      master
    (如果已经合并了怎么办?只能版本回退了。)

    推送分支

    ubuntu@myUbuntu:~/joe/learngit$ git remote    //查看远程连接
    origin
    ubuntu@myUbuntu:~/joe/learngit$ git remote -v    //显示详细的信息
    origin    git@github.com:joesGit15/learngit.git (fetch)
    origin    git@github.com:joesGit15/learngit.git (push)
    ubuntu@myUbuntu:~/joe/learngit$ git status
    位于分支 master
    您的分支领先 'origin/master'2 个提交。
      (使用 "git push" 来发布您的本地提交)
    无文件要提交,干净的工作区
    ubuntu@myUbuntu:~/joe/learngit$ git push origin master    //推送主分支
    ubuntu@myUbuntu:~/joe/learngit$ git push origin dev    //推送dev分支

    并不是一定要把本地分支往远程推送,可以参考如下内容:

    1. master分支是主分支,因此要时刻与远程同步;
    2. dev分支是开发分支,团队所有成员都需要在上面工作,所以也需要与远程同步;
    3. bug分支只用于在本地修复bug,就没必要推到远程了,除非老板要看看你每周到底修复了几个bug;
    4. feature分支是否推到远程,取决于你是否和你的小伙伴合作在上面开发。

  • 相关阅读:
    类加载器
    会话机制
    数据库读写分离
    代码优化工具
    杀毒软件框架设计
    树状结构
    spring依赖注入
    never stop believe yourself
    利用callKit实现电话防骚扰
    mac上使用使用rz,sz命令
  • 原文地址:https://www.cnblogs.com/zi-xing/p/4496417.html
Copyright © 2011-2022 走看看