本Git系列內(nèi)容更多是基于廖雪峰老師的Git教程的個(gè)人筆記、總結(jié)和擴(kuò)展,如有錯(cuò)誤,請(qǐng)多多指正。
我們將GIt運(yùn)用到實(shí)際工作中,肯定會(huì)遇到修復(fù)Bug,我們來模擬一下這個(gè)過程
首先創(chuàng)建一個(gè)你的工作分支work,你在上面書寫并提交代碼
$ git checkout -b work
Switched to a new branch 'work'
現(xiàn)在,你完成了一個(gè)項(xiàng)目,新建一個(gè)txt文件,命名為work.txt,寫入以下內(nèi)容
1234556
這個(gè)文件模擬你的正規(guī)項(xiàng)目,并把它提交到work分支,并合并至主分支master
$ git add work.txt
$ git commit -m 'work.txt_v1.0'
[work f50c40d] work.txt_v1.0
1 file changed, 1 insertion(+)
create mode 100644 work.txt
$ git checkout master
Switched to branch 'master'
Your branch is ahead of 'origin/master' by 17 commits.
(use "git push" to publish your local commits)
$ git merge work
Updating 1bf1741..f50c40d
Fast-forward
work.txt | 1 +
1 file changed, 1 insertion(+)
完成之后,就可以回到work分支繼續(xù)工作
$ git checkout work
Switched to branch 'work'
我們開始了第2個(gè)項(xiàng)目的開發(fā),創(chuàng)建第二個(gè)文件work2.txt,寫入以下內(nèi)容
45678
你正在寫的時(shí)候,組長找到了你,說明了第一個(gè)項(xiàng)目中存在嚴(yán)重Bug,你多寫了一個(gè)5
利用Git的優(yōu)勢(shì),我們可以創(chuàng)建一個(gè)bug修復(fù)分支來修復(fù)bug,問題來了,此時(shí)你的第二個(gè)項(xiàng)目還沒有寫完提交,如果直接創(chuàng)建切換bug修復(fù)分支
$ git checkout -b flexbug
Switched to a new branch 'flexbug'
$ git status
On branch flexbug
Untracked files:
(use "git add <file>..." to include in what will be committed)
work2.txt
nothing added to commit but untracked files present (use "git add" to track)
你會(huì)發(fā)現(xiàn),你未完成的work2.txt也會(huì)在flexbug分支中存在,很明顯這不是我希望看到的,這時(shí)候就需要使用git stash來將工作現(xiàn)場(chǎng)儲(chǔ)存起來,讓我們切換回work分支
$ git checkout work
Switched to branch 'work'
先將work2.txt提交
$ git add work2.txt
此時(shí)使用git stash
$ git stash
Saved working directory and index state WIP on work: f50c40d work.txt_v1.0
此時(shí)查看git status就會(huì)發(fā)現(xiàn)工作區(qū)是干凈的
$ git status
On branch work
nothing to commit, working tree clean
現(xiàn)在就可以放心的創(chuàng)建切換分支了,因?yàn)樯衔奈覀円呀?jīng)創(chuàng)建了flexbug分支,我們直接切換即可
$ git checkout bug
Switched to branch 'flexbug'
你現(xiàn)在可以返回git目錄,會(huì)發(fā)現(xiàn)work2.txt已經(jīng)不見了,現(xiàn)在就可以修復(fù)work.txt中的bug了,修改work.txt中內(nèi)容
123456
提交合并分支
$ git add work.tx
$ git commit -m 'work.txt_v1.1_fb'
[flexbug 49b54b0] work.txt_V1.1_fb
1 file changed, 1 insertion(+), 1 deletion(-)
$ git checkout master
Switched to branch 'master'
Your branch is ahead of 'origin/master' by 18 commits.
(use "git push" to publish your local commits)
$ git merge --no-ff -m 'flexBug work.txt_v1.1' flexbug
Merge made by the 'recursive' strategy.
work.txt | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
此時(shí)打開work.txt就可以發(fā)現(xiàn)bug已經(jīng)被修復(fù)了
123456
現(xiàn)在我們就可以刪除flexbug分支了
$ git branch -d flexbug
Deleted branch flexbug (was 49b54b0).
修復(fù)完bug之后,我們應(yīng)該回到work分支繼續(xù)工作了
$ git checkout work
Switched to branch 'work'
那之前的work2.txt去哪了?可以使用git stash list查看
$ git stash list
stash@{0}: WIP on work: f50c40d work.txt_v1.0
怎么找回之前的work2.txt呢,可以使用git stash pop
$ git stash pop
On branch work
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file: work2.txt
Dropped refs/stash@{0} (6db30b984686663b28c682db7d8e73066ddcb3c7)
再打開git目錄,就可以發(fā)現(xiàn)work2.txt已經(jīng)回來了