---恢復內容開始---git
軟件開發中,bug就像屢見不鮮同樣,有了bug就須要修復,在Git中,因爲分支是如此強大,因此,每一個bug均可以經過一個臨時分支來修復,修復後,合併分支,而後將臨時分支刪除。app
當你接到一個修復一個代號101的bug任務時,很天然地,你想建立一個分支issue-101來修復它,可是,等等,當前正在dev上進行的工做尚未提交:spa
$ git status
On branch master
Your branch is up to date with 'origin/master'.開發
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)it
new file: hello.pyio
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)ast
modified: readme.txt軟件
並非你不想提交,而是工做只進行到一半,還沒發提交,預計完成還需1天時間,可是,必須在兩個小時以內修復該bug,怎麼辦?date
幸虧,Git還提供了一個stash功能,能夠把當前工做現場「儲藏」起來,等之後恢復現場後繼續工做file
$ git stash
Saved working directory and index state WIP on dev: 45e3302 add merge
如今,用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 up to date with 'origin/master'.
$ git checkout -b issue-101
Switched to a new branch 'issue-101'
如今修復bug,須要把readme.txt裏面的內容「Git is software...」改成「Git is a free software....」而後提交
$ git add readme.txt
$ git commit -m "fix bug 101"
[issue-101 27c08c6] fix bug 101
1 file changed, 1 insertion(+), 1 deletion(-)
修復完成之後,切換到master分支,併合並完成,最後刪除issue-101
$ git checkout master
Switched to branch 'master'
Your branch is up to date with 'origin/master'.
$ git merge --no-ff -m "merged bug fix 101" issue-101
Merge made by the 'recursive' strategy.
readme.txt | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
太棒了,原計劃兩個小時的bug修復只花了五分鐘,如今,是時候接着回到dev分支幹活了
$ git checkout dev
Switched to branch 'dev'
$ git status
On branch dev
nothing to commit, working tree clean
工做區是乾淨的,剛纔的工做現場存到哪裏去了?用git stash list命令看看:
$ git stash list
stash@{0}: WIP on dev: 45e3302 add merge
工做現場還在,Git把stash內容存到某個地方了,可是須要恢復一下,有兩個辦法:
一是用git stash apply恢復,可是恢復後,stash內容並不刪除,你須要用git stash drop來刪除;
另外一種方式是用git stash pop,恢復的同時把stash內容也刪了。
$ git stash pop
On branch dev
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file: hello.py
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.txt
Dropped refs/stash@{0} (b0d65dce977ac8b90006982914b08c161363c942)
再用git stash list查看,就看不到任何stash內容了:
$ git stash list
你能夠屢次stash,恢復的時候,先用git stash list查看,而後恢復指定的stash,用命令:
$git stash apply stash@{0}
摘抄自:
https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000/00137602359178794d966923e5c4134bc8bf98dfb03aea3000