星空网 > 软件开发 > ASP.net

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* masterubuntu@myUbuntu:~/joe/learngit$ 
git checkout dev
  //我们正在dev上面修改文件切换到分支 'dev'ubuntu@myUbuntu:~/joe/learngit$ lsabc.c readme.txtubuntu@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 devHEAD 现在位于 b961f85 devubuntu@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 okubuntu@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 devubuntu@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.cubuntu@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  //查看远程连接originubuntu@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分支是否推到远程,取决于你是否和你的小伙伴合作在上面开发。




原标题:Git学习笔记(6)——Bug和Feature分支

关键词:Git

Git
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流