軟件開發中,bug就像屢見不鮮同樣。有了bug就須要修復,在Git中,因爲分支是如此的強大,因此每一個bug均可以經過一個新的臨時分支來修復,修復後,合併分支,而後將臨時分支刪除。git
切換回dev
分支app
lwenhaodeMacBook-Pro:TestGit lwenhao$ git checkout dev Switched to branch 'dev'
修改README.md
內容:學習
lwenhaodeMacBook-Pro:TestGit lwenhao$ cat README.md # TestGit 建立一個"dev"分支,我來操做。 學習分支管理策略。 編輯了dev分支上的內容,如今準備學習bug分支 lwenhaodeMacBook-Pro:TestGit lwenhao$
當你找到一個openfire的bug的任務時,你想建立一個分支issue-openfire
來修復它,可是,當前正在dev
上進行的工做尚未提交:.net
lwenhaodeMacBook-Pro:TestGit lwenhao$ git status 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: README.md no changes added to commit (use "git add" and/or "git commit -a") lwenhaodeMacBook-Pro:TestGit lwenhao$
並非你不想提交,而是工做只進行到一半,還無法提交,預計完成還需1天時間。可是,必須在兩個小時內修復該bug,怎麼辦?code
幸虧,Git還提供了一個stash
功能,能夠把當前工做現場「儲藏」起來,等之後恢復現場後繼續工做:blog
lwenhaodeMacBook-Pro:TestGit lwenhao$ git stash Saved working directory and index state WIP on dev: 8faa495 add 學習 lwenhaodeMacBook-Pro:TestGit lwenhao$
如今,用git status
查看工做區,就是乾淨的,所以能夠放心地建立分支來修復bug。開發
lwenhaodeMacBook-Pro:TestGit lwenhao$ git status On branch dev nothing to commit, working tree clean lwenhaodeMacBook-Pro:TestGit lwenhao$
首先肯定要在哪一個分支上修復bug,假定須要在master
分支上修復,就從master
建立臨時分支:get
lwenhaodeMacBook-Pro:TestGit lwenhao$ git checkout master Switched to branch 'master' Your branch is ahead of 'origin/master' by 6 commits. (use "git push" to publish your local commits) lwenhaodeMacBook-Pro:TestGit lwenhao$ git checkout -b issue-openfire Switched to a new branch 'issue-openfire' lwenhaodeMacBook-Pro:TestGit lwenhao$
如今修復bug,須要把README.md
中的「dev」改成「master」it
lwenhaodeMacBook-Pro:TestGit lwenhao$ cat README.md # TestGit 建立一個"master"分支,我來操做。 學習分支管理策略。 lwenhaodeMacBook-Pro:TestGit lwenhao$
而後提交:io
lwenhaodeMacBook-Pro:TestGit lwenhao$ git add README.md lwenhaodeMacBook-Pro:TestGit lwenhao$ git commit -m "dev to master" [issue-openfire c0e8f25] dev to master 1 file changed, 1 insertion(+), 1 deletion(-) lwenhaodeMacBook-Pro:TestGit lwenhao$
修復完成後,切換到master
分支,並完成合並,最後刪除issue-openfire
分支:
lwenhaodeMacBook-Pro:TestGit lwenhao$ git checkout master Switched to branch 'master' Your branch is ahead of 'origin/master' by 6 commits. (use "git push" to publish your local commits) lwenhaodeMacBook-Pro:TestGit lwenhao$ git merge --no-ff -m "merged bug fix openfire" issue-openfire Merge made by the 'recursive' strategy. README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) lwenhaodeMacBook-Pro:TestGit lwenhao$
原計劃兩個小時的bug修復只花了5分鐘!如今,是時候接着回到dev
分支幹活了!
lwenhaodeMacBook-Pro:TestGit lwenhao$ git checkout dev Switched to branch 'dev' lwenhaodeMacBook-Pro:TestGit lwenhao$ git status On branch dev nothing to commit, working tree clean lwenhaodeMacBook-Pro:TestGit lwenhao$
工做區是乾淨的,剛纔的工做現場存到哪去了?用git stash list
命令看看:
lwenhaodeMacBook-Pro:TestGit lwenhao$ git stash list stash@{0}: WIP on dev: 8faa495 add 學習 lwenhaodeMacBook-Pro:TestGit lwenhao$
工做現場還在,Git把stash內容存在某個地方了,可是須要恢復一下,有兩個辦法:
一是用git stash apply
恢復,可是恢復後,stash內容並不刪除,你須要用git stash drop
來刪除;
另外一種方式是用git stash pop
,恢復的同時把stash內容也刪了:
lwenhaodeMacBook-Pro:TestGit lwenhao$ git stash pop 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: README.md no changes added to commit (use "git add" and/or "git commit -a") Dropped refs/stash@{0} (97050754b252007d69e787968cccfa46f54f7f31) lwenhaodeMacBook-Pro:TestGit lwenhao$
再用git stash list
查看,就看不到任何stash內容了:
lwenhaodeMacBook-Pro:TestGit lwenhao$ git stash list lwenhaodeMacBook-Pro:TestGit lwenhao$
你能夠屢次stash,恢復的時候,先用git stash list
查看,而後恢復指定的stash,用命令:
git stash apply stash@{0}
修復bug時,咱們會經過建立新的bug分支進行修復,而後合併,最後刪除;
當手頭工做沒有完成時,先把工做現場git stash
一下,而後去修復bug,修復後,再git stash pop
,回到工做現場。
繼續閱讀:07 分支管理 —— Feature分支