Git學習筆記(4)

Bug分支暫時儲存當前工作,重新分配bug分支完成修復

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

$ git stash

Saved working directory and index state WIP on dev: 6224937 add merge

HEAD is now at 6224937 add merge

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

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

$ git checkout master

Switched to branch 'master'

Your branch is ahead of 'origin/master' by 6 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 cc17032] fix bug 101

 1 file changed, 1 insertion(+), 1 deletion(-)

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

$ git checkout master

Switched to branch 'master'

Your branch is ahead of 'origin/master' by 2 commits.

$ git merge --no-ff -m "merged bug fix 101" issue-101

Merge made by the 'recursive' strategy.

 readme.txt |    2 +-

 1 file changed, 1 insertion(+), 1 deletion(-)

$ git branch -d issue-101

Deleted branch issue-101 (was cc17032).

太棒了,原計劃兩個小時的bug修復只花了5分鐘!現在,是時候接着回到dev分支幹活了!

$ git checkout dev

Switched to branch 'dev'

$ git status

# On branch dev

nothing to commit (working directory clean)

工作區是乾淨的,剛纔的工作現場存到哪去了?用git stash list命令看看:

$ git stash list

stash@{0}: WIP on dev: 6224937 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} (f624f8e5f082f2df2bed8a4e09c12fd2943bdd40)

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

$ git stash list

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

$ git stash apply stash@{0}

Feature分支:添加與刪除額外加入的新功能

你終於接到了一個新任務:開發代號爲Vulcan的新功能,該功能計劃用於下一代星際飛船。

於是準備開發:

$ git checkout -b feature-vulcan

Switched to a new branch 'feature-vulcan'

5分鐘後,開發完畢:

$ git add vulcan.py

$ git status

# On branch feature-vulcan

# Changes to be committed:

#   (use "git reset HEAD <file>..." to unstage)

#

#       new file:   vulcan.py

#

$ git commit -m "add feature vulcan"

[feature-vulcan 756d4af] add feature vulcan

 1 file changed, 2 insertions(+)

 create mode 100644 vulcan.py

切回dev,準備合併:

$ git checkout dev

一切順利的話,feature分支和bug分支是類似的,合併,然後刪除。

但是,

就在此時,接到上級命令,因經費不足,新功能必須取消!

雖然白乾了,但是這個分支還是必須就地銷燬:

$ 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分支還沒有被合併,如果刪除,將丟失掉修改,如果要強行刪除,需要使用命令git branch -D feature-vulcan。

現在我們強行刪除:

$ git branch -D feature-vulcan

Deleted branch feature-vulcan (was 756d4af).

發佈了51 篇原創文章 · 獲贊 19 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章