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 分支名稱 將指定分支合並到當前分支