軟件開發中,bug就像家常便飯一樣。有了bug就需要修復,在Git中,由於分支是如此的強大,所以,每個bug都可以通過一個新的臨時分支來修復,修復後,合併分支,然後將臨時分支刪除。
當你接到一個修復一個代號101的bug的任務時,很自然地,你想創建一個分支issue-101來修復它,但是,等等,當前正在dev上進行的工作還沒有提交:
並不是你不想提交,而是工作只進行到一半,還沒法提交,預計完成還需1天時間。但是,必須在兩個小時內修復該bug,怎麼辦?
幸好,Git還提供了一個stash功能,可以把當前工作現場“儲藏”起來,等以後恢復現場後繼續工作:
$ git branch
* dev
master
$ git status #修改t1.txt 不要提交,這是dev分支上
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: t1.txt
no changes added to commit (use "git add" and/or "git commit -a")
$ git stash #把現場儲存起來
Saved working directory and index state WIP on dev: cd2ba01 br
HEAD is now at cd2ba01 br
$ git checkout master #切到主分支上
$ git checkout -b issue-102 #在主分支上創建新的bug 分支來修復bug
$ vi t1.txt #修改文件
$ git add t1.txt
$ git commit -m "fix bug"
$ git checkout master #切換到主線
$ git merge --no-ff -m "merged bug fix 102" issue-102 #合併分支到主線
$ git branch -d issue-102 #放心的刪除分支
$git checkout dev
$git status #查看狀態
$ git stash list #查看儲存了哪些
stash@{0}: WIP on dev: cd2ba01 br
stash@{1}: WIP on dev: cd2ba01 br
$ git stash pop #恢復的同時把stash內容也刪了
$ cat t1.txt
刪除分支:
$ git branch -d feature-vulcan #普通刪除分支
$ git branch -D feature-vulcan #強行刪除分支