Git 开发新的功能分支

时间:2023-03-09 20:02:56
Git 开发新的功能分支

软件开发中,总有无穷无尽的新的功能要不断的添加进来。添加一个新功能时,你肯定不希望因为一些实验性质的代码把主分支搞乱了,

所以每添加一个新功能,最好新建一个feature分支,在上面开发,完成后,合并,最后,删除该feature分支。

现在你接到一个新的任务:开发代号为Faster的新功能,于是准备开发:

LV@LV-PC MINGW32 /c/gitskill (dev)
$ git checkout -b feature-faster
A hello.txt
M readme.txt
Switched to a new branch 'feature-faster'

LV@LV-PC MINGW32 /c/gitskill (feature-faster)
$ ls
hello.txt README.md readme.txt

LV@LV-PC MINGW32 /c/gitskill (feature-faster)
$ vim faster.txt

LV@LV-PC 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.

LV@LV-PC 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分支是类似的,合并,然后删除。

但是,就在此时,接到上级的命令,因为经费不足,新功能必须取消,

虽然白干了,但是这个分支还是必须得就地销毁:

LV@LV-PC 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命令。

LV@LV-PC MINGW32 /c/gitskill (dev)
$ git branch -D feature-faster
Deleted branch feature-faster (was d84bbe4).

小结:如果要开发新的功能,最好新建一个分支;

如果要丢弃一个没有被合并过的分支,可以通过git branch -D feature-faster来强行删除