添加一個(gè)新功能時(shí),不想把主分支搞亂。所以,每添加一個(gè)新功能,最好新建一個(gè)feature分支,在上面開(kāi)發(fā),完成后,合并,最后,刪除該feature分支。
eg:現(xiàn)在,加一個(gè)Vulcan的新功能,該功能計(jì)劃用于下一代星際飛船。
先添加feature-vulcan分支
$ git checkout -b feature-vulcan
Switched to a new branch 'feature-vulcan'
開(kāi)發(fā)完畢:
$ git add vulcan.py
$ git status
On branch feature-vulcan
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file: hello.py
new file: vulcan.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.md
切回dev,準(zhǔn)備合并:
$ git checkout dev
一切順利的話,feature分支和bug分支是類(lèi)似的,合并,然后刪除。
但是!
就在此時(shí),接到上級(jí)命令,因經(jīng)費(fèi)不足,新功能必須取消!
雖然白干了,但是這個(gè)包含機(jī)密資料的分支還是必須就地銷(xiāo)毀:
$ git branch -d feature-vulcan
error: The branch 'feature-vulcan' is not fully merged.
If you are sure you want to delete it, run 'git branch -D feature-vulcan'.
銷(xiāo)毀失敗。Git友情提醒,feature-vulcan分支還沒(méi)有被合并,如果刪除,將丟失掉修改,如果要強(qiáng)行刪除,需要使用大寫(xiě)的-D參數(shù)。。
現(xiàn)在我們強(qiáng)行刪除:
$ git branch -D feature-vulcan
Deleted branch feature-vulcan (was 287773e).
終于刪除成功!
小結(jié)
開(kāi)發(fā)一個(gè)新feature,最好新建一個(gè)分支;
如果要丟棄一個(gè)沒(méi)有被合并過(guò)的分支,可以通過(guò)git branch -D <name>強(qiáng)行刪除。