做爲程序員,咱們時常遇到這樣的場景,公司的產品上線了,程序員們美滋滋的開始開發新功能但願獲得更多的流量。這時候,公司上線的產品發生了很嚴重的bug,但是咱們已經在這個bug的基礎上將新功能開發了一半怎麼辦?html
這時候就要用到Git的bug解決方案。git
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 時候就會出現衝突,有衝突解決衝突便可。django
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相關經常使用命令:vim
分支學習:branch稱爲分支,默認僅有一個名爲master的分支。通常開發新功能流程爲:開發新功能時會在分支dev上進行,開發完畢後再合併到master分支。app
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的時候,就能夠這麼來作:ide
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相關經常使用命令:this