原文:https://blog.csdn.net/abc15766228491/article/details/79211593
先新建一個分支(方法前一節課有哦),然後。。就可以做今天的實驗啦
軟件開發中,bug就像家常便飯一樣。有了bug就需要修復,在Git中,由於分支是如此的強大,所以,每個bug都可以通過一個新的臨時分支來修復,修復後,合併分支,然後將臨時分支刪除。
當你接到一個修復一個代號101的bug的任務時,很自然地,你想創建一個分支issue-101來修復它,但是,等等,當前正在dev上進行的工作還沒有提交:
$ 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
no changes added to commit (use "git add" and/or "git commit -a")
並不是你不想提交,而是工作只進行到一半,還沒法提交,預計完成還需1天時間。但是,必須在兩個小時內修復該bug,怎麼辦
幸好,Git還提供了一個stash功能,可以把當前工作現場“儲藏”起來,等以後恢復現場後繼續工作:
$ git stash
Saved working directory and index state WIP on dev: d50594a merge with no-ff
現在,用git status查看工作區,就是乾淨的(除非有沒有被Git管理的文件),因此可以放心地創建分支來修復bug。
$ git status
On branch dev
nothing to commit, working tree clean
首先,確定哪個分支上修復bug,假定需要在master分支上修復,就從master創建臨時分支:
$ git checkout master
Switched to branch 'master'
Your branch is ahead of 'origin/master' by 8 commits.
(use "git push" to publish your local commits)
$ git checkout -b issus-101
Switched to a new branch 'issus-101'
現在修復bug,將readme裏面第一句前面加一句helloworld吧,然後,提交
$ git add readme
$ git commit -m "fix bug 101"
[issus-101 ce34493] fix bug 101
1 file changed, 1 insertion(+)
然後,切換到master分支,然後,刪除這個分支
$ git checkout master
Switched to branch 'master'
Your branch is ahead of 'origin/master' by 8 commits.
(use "git push" to publish your local commits)
$ git merge --no-ff -m "merged bug fix 101" issus-101
Merge made by the 'recursive' strategy.
readme | 1 +
1 file changed, 1 insertion(+)
git branch -d issus-101
Deleted branch issus-101 (was ce34493).
bug修復完啦,回到dev分支繼續(這裏沒有合併,因此readme第一行沒有helloworld)
$ git checkout dev
Switched to branch 'dev'
$ git status
# On branch dev
nothing to commit (working directory clean)
剛纔的工作現場,用git stash list查看
$ git stash list
stash@{0}: WIP on dev: d50594a merge with no-ff
工作現場還在,Git把stash內容存在某個地方了,但是需要恢復一下,有兩個辦法:
一是:git stash apply 恢復,但是恢復之後,stash內容不刪除,需要用git stash drop刪除;
二是:git stash pop,恢復的同時把stash內容頁刪除了,再查看,就沒有內容了
$ 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
no changes added to commit (use "git add" and/or "git commit -a")
Dropped refs/stash@{0} (1661a1dad30698e82e1887ecbad6063c29ede7e1)
$ git stash list
你可以多次stash,恢復的時候,先用git stash list查看,然後恢復指定的stash,用命令:
$ git stash apply stash@{0}