[Git] 從任意一個 commit 節點,建立出新的分支
平常改完程式,就是用 git commit 將東西儲存起來,
不過有時會突然遇到客戶遇到某個 bug,
必須從那個客戶使用的版本去修正,
而不能給他最新的版本 (因為可能已經有新的未測試功能)…
舉例來說,這是我執行 git log master
的結果:
$ git log master commit daa64e93f5904018291e508751e9bbd8d59d411e (HEAD -> master, origin/master, origin/HEAD) Merge: bf8be04 f6b54d8 Author: NoName <[email protected]> Date: Mon Dec 27 00:40:28 2021 +0800 Merge pull request #77 from feature/separate-tool Separate tool from CLI commit 2d5351c574adb663064754070ad6ba8eae97f37d Author: NoName <[email protected]> Date: Wed Dec 22 02:11:07 2021 +0800 Separate tool from CLI commit bf8be0481237cb119e5b4008c96f7f9776aa1e17 Merge: 34246ac 99774da Author: NoName <[email protected]> Date: Wed Dec 22 00:51:42 2021 +0800 Merge pull request #76 from feature/separate-ptn Fix value of patched commit 99774daa10a5eef62c61bb0ff8893b2fa559c29b Author: NoName <[email protected]> Date: Tue Dec 21 22:10:23 2021 +0800 Fix value of patched
從新到舊,有這些 commit:
- daa64e93f5904018291e508751e9bbd8d59d411e (master)
- 2d5351c574adb663064754070ad6ba8eae97f37d
- bf8be0481237cb119e5b4008c96f7f9776aa1e17
- 99774daa10a5eef62c61bb0ff8893b2fa559c29b
假設我想從 bf8be0481237cb119e5b4008c96f7f9776aa1e17 這個 commit,
建立並切換到新的分支 feature/rollback-and-update-ptn
的話,可以像下面這樣下:
$ git checkout -b feature/rollback-and-update-ptn bf8be0481237cb119e5b4008c96f7f9776aa1e17 Switched to a new branch 'feature/rollback-and-update-ptn'
再次執行 git log
指令,
可以發現目前就是在 bf8be0481237cb119e5b4008c96f7f9776aa1e17 這個 commit 上囉:
$ git log commit bf8be0481237cb119e5b4008c96f7f9776aa1e17 (HEAD -> feature/rollback-and-update-ptn) Merge: 34246ac 99774da Author: NoName <[email protected]> Date: Wed Dec 22 00:51:42 2021 +0800 Merge pull request #76 from feature/separate-ptn Fix value of patched
這樣就可以在這個分支上,修正 bug 再出修正版給客戶了。
當然,之後的修正也可以再合併 (merge) 回 master 囉~
(本頁面已被瀏覽過 108 次)