Git--公司bug解决篇
作为程序员,我们时常遇到这样的场景,公司的产品上线了,程序员们美滋滋的开始开发新功能希望得到更多的流量。这时候,公司上线的产品发生了很严重的bug,可是我们已经在这个bug的基础上将新功能开发了一半怎么办?
这时候就要用到Git的bug解决方案。
方案一:stash
stash用于将工作区发生变化的所有文件获取临时存储在“某个地方”,将工作区还原当前版本未操作前的状态;stash还可以将临时存储在“某个地方”的文件再次拿回到工作区。
acBook-Pro-4:pondo gaoshengyue$ vim app01/views.py # 开发灭霸功能,刚开发到一半 MacBook-Pro-4:pondo gaoshengyue$ git status #查看一下状态 On branch master 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: app01/views.py no changes added to commit (use "git add" and/or "git commit -a") MacBook-Pro-4:pondo gaoshengyue$ git stash # 将开发到一半的灭霸功能,临时存储到“某个地方” Saved working directory and index state WIP on master: 0972f4b 复仇者联盟上线 HEAD is now at 0972f4b 复仇者联盟上线 MacBook-Pro-4:pondo gaoshengyue$ git status # 工作区回到当前版本未做任何操作前 On branch master nothing to commit, working tree clean ###回滚 MacBook-Pro-4:pondo gaoshengyue$ vim pondo/settings.py # 紧急修复bug MacBook-Pro-4:pondo gaoshengyue$ git status On branch master 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: pondo/settings.py no changes added to commit (use "git add" and/or "git commit -a") MacBook-Pro-4:pondo gaoshengyue$ git add . # 添加到修改bug的代码到暂存状态 MacBook-Pro-4:pondo gaoshengyue$ git commit -m '紧急修复bug' # 提交修复Bug的代码到分支 [master 1300d33] 紧急修复bug 1 file changed, 1 insertion(+) MacBook-Pro-4:pondo gaoshengyue$ git stash pop # 将开发到一半的灭霸功能从“某个地方”再次拿会工作区继续开发 On branch master 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: app01/views.py no changes added to commit (use "git add" and/or "git commit -a") Dropped refs/stash@{0} (059d78ca8fa204f9559bd3ce0ae76235969b4301)
特别的:执行 git stash pop 命令时,可能会遇到冲突,因为在紧急修复bug的代码和通过stash存储在“某个地方”的代码会有重合部分,所以执行 git stash pop 时候就会出现冲突,有冲突解决冲突即可。
a. 原来内容: from django.shortcuts import render,HttpResponse def index(request): return render(request,'index.html') def africa(request): return HttpResponse('复仇者联盟') b. 开发到一半直播功能: from django.shortcuts import render,HttpResponse def index(request): return render(request,'index.html') def africa(request): return HttpResponse('复仇者联盟') def live(request): print('灭霸开发到一半') return HttpResponse('....') c. 执行git stash,回到当前版本未修改状态: from django.shortcuts import render,HttpResponse def index(request): return render(request,'index.html') def africa(request): return HttpResponse('复仇者联盟') d. 修复Bug并提交: from django.shortcuts import render,HttpResponse def index(request): return render(request,'index.html') def africa(request): return HttpResponse('复仇者联盟金刚狼') e. 继续开发直播功能 git stash pop,此时会出现冲突: MacBook-Pro-4:pondo gaoshengyue$ git stash pop Auto-merging app01/views.py CONFLICT (content): Merge conflict in app01/views.py 表示app01/views.py存在冲突需要解决,此时文件内容为: from django.shortcuts import render,HttpResponse def index(request): return render(request,'index.html') def africa(request): <<<<<<< Updated upstream: # 修复Bug时更改的内容 return HttpResponse('复仇者联盟金刚狼') ======= # 修复Bug前正在开发新功能时的内容 return HttpResponse('复仇者联盟') def live(request): print('灭霸刚开发到一半') return HttpResponse('灭霸') >>>>>>> Stashed changes 需要自行解决冲突,然后继续开发,如: from django.shortcuts import render,HttpResponse def index(request): return render(request,'index.html') def africa(request): return HttpResponse('复仇者联盟金刚狼') def live(request): print('灭霸刚开发到一半') return HttpResponse('灭霸')
stash相关常用命令:
- git stash 将当前工作区所有修改过的内容存储到“某个地方”,将工作区还原到当前版本未修改过的状态
- git stash list 查看“某个地方”存储的所有记录
- git stash clear 清空“某个地方”
- git stash pop 将第一个记录从“某个地方”重新拿到工作区(可能有冲突)
- git stash apply 编号, 将指定编号记录从“某个地方”重新拿到工作区(可能有冲突)
- git stash drop 编号,删除指定编号的记录
方案二:branch
分支学习:branch称为分支,默认仅有一个名为master的分支。一般开发新功能流程为:开发新功能时会在分支dev上进行,开发完毕后再合并到master分支。
MacBook-Pro-4:pondo gaoshengyue$ git branch dev # 创建新分支,即:拷贝一份当前所在分支代码到新分支 MacBook-Pro-4:pondo gaoshengyue$ git checkout dev # 切换到dev分支 MacBook-Pro-4:pondo gaoshengyue$ vim app01/views.py # 开发功能 MacBook-Pro-4:pondo gaoshengyue$ git status # 查看状态,即:在dev分支修改了app01/views.py文件 On branch dev 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: app01/views.py no changes added to commit (use "git add" and/or "git commit -a") MacBook-Pro-4:pondo gaoshengyue$ git add . # 将修改文件添加到版本库的暂存区 MacBook-Pro-4:pondo gaoshengyue$ git commit -m '新功能开发完毕' # 将暂存区的内容提交到当前所在分支,即:dev分支 [dev 32b40cd] 新功能开发完毕 1 file changed, 2 insertions(+) MacBook-Pro-4:pondo gaoshengyue$ git checkout master # 切换回master分支 Switched to branch 'master' MacBook-Pro-4:pondo gaoshengyue$ git merge dev # 将dev分支内容合并到master分支 Updating 0972f4b..32b40cd Fast-forward app01/views.py | 2 ++ 1 file changed, 2 insertions(+)
按照分支的思路,如果我们在公司产品上线遇到bug的时候,就可以这么来做:
MacBook-Pro-4:pondo gaoshengyue$ git branch # 当前在master分支 * master MacBook-Pro-4:pondo gaoshengyue$ git branch dev # 创建dev分支用于开发新功能 MacBook-Pro-4:pondo gaoshengyue$ git checkout dev # 切换到dev分支 Switched to branch 'dev' MacBook-Pro-4:pondo gaoshengyue$ vim app01/views.py # 开发新功能到一半,需要紧急修复Bug MacBook-Pro-4:pondo gaoshengyue$ git add . MacBook-Pro-4:pondo gaoshengyue$ git commit -m '新功能开发一半' [dev b3ac2cb] 新功能开发一半 1 file changed, 2 insertions(+) MacBook-Pro-4:pondo gaoshengyue$ git checkout master # 切换回master分支 Switched to branch 'master' MacBook-Pro-4:pondo gaoshengyue$ git branch bug # 创建bug分支 MacBook-Pro-4:pondo gaoshengyue$ git checkout bug # 切换到bug分支 Switched to branch 'bug' MacBook-Pro-4:pondo gaoshengyue$ vim pondo/settings.py # 修改bug MacBook-Pro-4:pondo gaoshengyue$ git add . # 提交bug MacBook-Pro-4:pondo gaoshengyue$ git commit -m '紧急修复bug' # 提交bug [bug f42f386] 紧急修复bug 1 file changed, 1 insertion(+), 1 deletion(-) MacBook-Pro-4:pondo gaoshengyue$ git checkout master # 切换会master Switched to branch 'master' MacBook-Pro-4:pondo gaoshengyue$ git merge bug # 将bug分支内容合并到master分支,表示bug修复完毕,可以上线 Updating 0972f4b..f42f386 Fast-forward pondo/settings.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) MacBook-Pro-4:pondo gaoshengyue$ git checkout dev # 切换到dev分支,继续开发新功能 Switched to branch 'dev' MacBook-Pro-4:pondo gaoshengyue$ vim app01/views.py # 继续开发其他一半功能 MacBook-Pro-4:pondo gaoshengyue$ git add . # 提交新功能 MacBook-Pro-4:pondo gaoshengyue$ git commit -m '继续开发完成' # 提交功能 [dev c0bfb27] 继续开发完成 1 file changed, 1 insertion(+) MacBook-Pro-4:pondo gaoshengyue$ git checkout master # 切换回master分支 Switched to branch 'master' MacBook-Pro-4:pondo gaoshengyue$ git merge dev # 将dev分支合并到master分支 Merge made by the 'recursive' strategy. app01/views.py | 3 +++ 1 file changed, 3 insertions(+)
注意:git merge 时也可能会出现冲突,解决冲突的方式上述stash相同,即:找到冲突文件,手动修改冲突并提交。
branch相关常用命令:
- git branch 分支名称 创建分支
- git checkout 分支名称 切换分支
- git branch -m 分支名称 创建并切换到指定分支
- git branch 查看所有分支
- git branch -d 分支名称 删除分支
- git merge 分支名称 将指定分支合并到当前分支