在還沒把開發的分支合併到 main
,就不小心在本地跟遠端把這個分支刪掉的話,怎麼辦?
流程解釋
輸入:
git reflog
25xxxxx HEAD@{1}: checkout: moving from feature/pie to main
c7xxxxx HEAD@{2}: commit: fix: echart dispose
a2xxxxx HEAD@{3}: commit: feat: set height
08xxxxx HEAD@{4}: commit: feat: basic pie component
25xxxxx HEAD@{5}: checkout: moving from main to feature/pie
這是用來看我在本地端曾經有哪些 commit 記錄。
這裡面我做的動作依序是:
HEAD@{5}
:把我所在的分支從main
移動到feature/pie
HEAD@{4} - HEAD@{2}
:我在feature/pie
推了三個 commitHEAD@{1}
:把我所在的分支從feature/pie
切回main
分支
git checkout -b feature/pie c7xxxxx
也就是我要創建一個 feature/pie
分支,切換到它,並且以 c7xxxxx
這個 commit 為我這個分支的起始點。
也就是 c7xxxxx
之前同個分支 commit 都會包含在前面,然後我接下來就是接續 c7xxxxx
開始 commit 跟推 code。
✨「每個 commit 都會記錄上一個 commit 是哪一個,所以只要你知道某個 commit,就可以找出該分支上的所有記錄。」
為什麼分支被刪掉還可以救回來?
Git 裡面分支名稱不是重點,Commit 才是,反正延伸出去的分支到後來也都會被刪掉。
所以只要有 Commit,就會存在 Git 的資料庫裡面,隨時可以回去找。