標籤:
軟體開發中,總有無窮無盡的新的功能要不斷的添加進來。添加一個新功能時,你肯定不希望因為一些實驗性質的代碼把主分支搞亂了,
所以每添加一個新功能,最好建立一個feature分支,在上面開發,完成後,合并,最後,刪除該feature分支。
現在你接到一個新的任務:開發代號為Faster的新功能,於是準備開發:
[email protected] MINGW32 /c/gitskill (dev)
$ git checkout -b feature-faster
A hello.txt
M readme.txt
Switched to a new branch ‘feature-faster‘
[email protected] MINGW32 /c/gitskill (feature-faster)
$ ls
hello.txt README.md readme.txt
[email protected] MINGW32 /c/gitskill (feature-faster)
$ vim faster.txt
[email protected] MINGW32 /c/gitskill (feature-faster)
$ git add faster.txt
warning: LF will be replaced by CRLF in faster.txt.
The file will have its original line endings in your working directory.
[email protected] MINGW32 /c/gitskill (feature-faster)
$ git commit -m "faster branch commit"
[feature-faster d84bbe4] faster branch commit
warning: LF will be replaced by CRLF in faster.txt.
The file will have its original line endings in your working directory.
2 files changed, 2 insertions(+)
create mode 100644 faster.txt
create mode 100644 hello.txt
切回dev準備合并分支,一切順利的話,feature分支和bug分支是類似的,合并,然後刪除。
但是,就在此時,接到上級的命令,因為經費不足,新功能必須取消,
雖然白乾了,但是這個分支還是必須得就地銷毀:
[email protected] MINGW32 /c/gitskill (dev)
$ git branch -d feature-faster
error: The branch ‘feature-faster‘ is not fully merged.
If you are sure you want to delete it, run ‘git branch -D feature-faster‘.
沒有合并前,不能刪除。如果要強制移除需要使用git branch -D feature-faster命令。
[email protected] MINGW32 /c/gitskill (dev)
$ git branch -D feature-faster
Deleted branch feature-faster (was d84bbe4).
小結:如果要開發新的功能,最好建立一個分支;
如果要丟棄一個沒有被合并過的分支,可以通過git branch -D feature-faster來強行刪除
Git 開發新的功能分支