Git 分支的一些特殊的使用方式:Bug分支/feature分支/儲存現場/


參考鏈接:https://www.liaoxuefeng.com/wiki/896043488029600/900388704535136

一般都與dev分支進行合並

Bug分支

Bug分支也是一個分支,他甚至和前面創建的分支沒有區別,只是在Git中,分支是如此的強大,以至於在修復Bug的時候,所以,每個bug都可以通過一個新的臨時分支來修復,修復后,合並分支,然后將臨時分支刪除。所以

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

$ git checkout master
Switched to branch 'master'
Your branch is ahead of 'origin/master' by 6 commits.
  (use "git push" to publish your local commits)

$ git checkout -b issue-101
Switched to a new branch 'issue-101'

  現在修復bug,需要把“Git is free software ...”改為“Git is a free software ...”,然后提交:

$ git add readme.txt 
$ git commit -m "fix bug 101"
[issue-101 4c805e2] fix bug 101
 1 file changed, 1 insertion(+), 1 deletion(-)

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

保存現場

Bug分支在使用起來和之前的分支並沒有什么不同,依舊是眾所周知的創建/修改工作/添加/提交/返回master/合並/刪除bug分支.這並沒有什么好講的.

特殊的是一種在我們准備創建Bug分支的時候可能會遇到的一種意外情況,就是我們正在dev分支上的工作還沒有達到能夠提交的要求.即我們的工作區還有文件(使用git status 來查看是否有改動還沒有提交).而在我們創建新的分支的時候,操作的對象就是當前工作區中的內容.

$ git status
On branch dev
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

	new file:   hello.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.txt

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

$ git stash#這就好了
Saved working directory and index state WIP on dev: f52c633 add merge

  現在,用git status查看工作區,就是干凈的(除非有沒有被Git管理的文件),因此可以放心地創建分支來修復bug。

  當bug修復好了后.是時候接着回到dev分支干活了!

$ git checkout dev
Switched to branch 'dev'

$ git status
On branch dev
nothing to commit, working tree clean

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

$ git stash list
stash@{0}: WIP on dev: f52c633 add merge

  

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

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

另一種方式是用git stash pop,恢復的同時把stash內容也刪了:

$ git stash pop
On branch dev
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

	new file:   hello.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.txt

Dropped refs/stash@{0} (5d677e2ee266f39ea296182fb2354265b91b3b2a)

  再用git stash list查看,就看不到任何stash內容了:

  你可以多次stash,恢復的時候,先用git stash list查看,然后恢復指定的stash,用命令:

$ git stash apply stash@{0}

  

小結

修復bug時,我們會通過創建新的bug分支進行修復,然后合並,最后刪除;

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

Feature分支

參考鏈接:https://www.liaoxuefeng.com/wiki/896043488029600/900394246995648

這也是分支的另一種叫法

添加一個新功能時,你肯定不希望因為一些實驗性質的代碼,把主分支搞亂了,所以,每添加一個新功能,最好新建一個feature分支,在上面開發,完成后,合並,最后,刪除該feature分支。

有時也可能會遇到一些額外的情況,比如你在新建的Feature分支中已經把新功能寫好了,就在你切回了dev分支,正准備合並的時候,突然接到要求說這個功能取消了,即使寫好了,雖然白干了,但是這個包含機密資料的分支還是必須就地銷毀:

$ 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'.

  銷毀失敗。Git友情提醒,feature-vulcan分支還沒有被合並,如果刪除,將丟失掉修改,如果要強行刪除,需要使用大寫的-D參數。

  現在我們強行刪除:  

$ git branch -D feature-vulcan
Deleted branch feature-vulcan (was 287773e).

  

小結

開發一個新feature,最好新建一個分支;

如果要丟棄一個沒有被合並過的分支,可以通過git branch -D <name>強行刪除。

 


免責聲明!

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



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