Git的Bug分支----臨時保存現場git stash


軟件開發中,bug就像家常便飯一樣,有了bug就需要修復,在Git中,由於分支是如此的強大,所以每個bug通過一個新的分支來修復,在修復后,合並分支,然后將臨時分支刪除。

當你接到一個修復代號為119的bug時,很自然的想建立一個分支issue-119來修復它,但是,當前在dev上進行的工作還沒有提交。

LV@LV-PC MINGW32 /c/gitskill (dev)
$ git status
On branch dev
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)

new file: hello.txt

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

並不是你不想提交,而是工作進行到一半,還沒法提交,預計完成時間還需要半天的時間,但是你必須在一個小時內修復該bug,怎么辦?

幸運的是,Git還提供了一個stash功能,可以把當前工作現場“儲藏”起來,等以后恢復現場后繼續工作:

LV@LV-PC MINGW32 /c/gitskill (dev)
$ git stash
Saved working directory and index state WIP on dev: 4f4f23a dev commit
HEAD is now at 4f4f23a dev commit

現在用git status查看工作區,就是干凈的,因此可以放心的創建分支來修復Bug

LV@LV-PC MINGW32 /c/gitskill (dev)
$ git status
On branch dev
nothing to commit, working directory clean

首先確定在哪個分支上修復bug,假定需要在master分支上修復,就從master分支上創建臨時分支:

LV@LV-PC MINGW32 /c/gitskill (dev)
$ git checkout master
Switched to branch 'master'
Your branch is up-to-date with 'origin/master'.

LV@LV-PC MINGW32 /c/gitskill (master)
$ git checkout -b issue-119
Switched to a new branch 'issue-119'

現在修復bug,將在readme.txt中添加“bug修復完成”

$ cat readme.txt
master分支內容
添加dev分支內容
master分支上的合並測試內容
分支合並測試

dev分支--no-ff方式的merge
bug修復完成

 

LV@LV-PC MINGW32 /c/gitskill (issue-119)
$ git add readme.txt

LV@LV-PC MINGW32 /c/gitskill (issue-119)
$ git commit -m "bug 119 had fix"
[issue-119 b4d16ef] bug 119 had fix
1 file changed, 1 insertion(+)

修復完成后,切換到master分支,並完成合並,最后刪除issue-119

LV@LV-PC MINGW32 /c/gitskill (issue-119)
$ git checkout master
Switched to branch 'master'
Your branch is up-to-date with 'origin/master'.

LV@LV-PC MINGW32 /c/gitskill (master)
$ git merge --no-ff -m "merge bug fix 119" issue-119
Merge made by the 'recursive' strategy.
readme.txt | 1 +
1 file changed, 1 insertion(+)

LV@LV-PC MINGW32 /c/gitskill (master)
$ git branch -d issue-119
Deleted branch issue-119 (was b4d16ef).

bug已經修復了,現在是時候回到dev分支上干活了

LV@LV-PC MINGW32 /c/gitskill (master)
$ git checkout dev
Switched to branch 'dev'

LV@LV-PC MINGW32 /c/gitskill (dev)
$ git status
On branch dev
nothing to commit, working directory clean

工作區是干凈的,剛才的工作區存到哪里去了?用git stash list命令看看:

LV@LV-PC MINGW32 /c/gitskill (dev)
$ git stash list
stash@{0}: WIP on dev: 4f4f23a dev commit

工作現場還在,Git把stash內容存到某個地方了,但是需要恢復一下,有兩種方法:

1.用git stash apply恢復,但是恢復后,stash內容並不刪除,你需要用git stash drop來刪除。

2.用git stash pop,恢復的同時把stash內容也刪除了。

LV@LV-PC MINGW32 /c/gitskill (dev)
$ git stash list
stash@{0}: WIP on dev: 4f4f23a dev commit

LV@LV-PC MINGW32 /c/gitskill (dev)
$ git stash apply stash@{0}
On branch dev
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)

new file: hello.txt

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


LV@LV-PC MINGW32 /c/gitskill (dev)
$ git stash list
stash@{0}: WIP on dev: 4f4f23a dev commit

LV@LV-PC MINGW32 /c/gitskill (dev)
$ git stash drop stash@{0}
Dropped stash@{0} (8f8d8fbddf55ed37d505eeca7371f78e705a4daa)

LV@LV-PC MINGW32 /c/gitskill (dev)
$ git stash list

查看工作區:

LV@LV-PC MINGW32 /c/gitskill (dev)
$ git status
On branch dev
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)

new file: hello.txt

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)

 我們又回到了修復bug之前了

小結:我們修復bug時,我們創建新的bug分支進行修復,然后合並,最后刪除;

當手頭上的工作沒有完成時,先把工作現場git stash一下,然后去修復bug,修復后,再git stash pop ,回到工作現場。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM