項目開發中偶爾會碰到這種需求:須要把倉庫中的一個子目錄獨立出來,成爲一個全新的獨立倉庫。但又但願這個新倉庫能保留原來全部的提交記錄,方面之後查看。git
以下面的目錄結構所示,要將項目倉庫中 component_a 目錄的代碼抽取出來,獨立成新倉庫。shell
- project_root |- src |- components |- component_a |- component_b
component_a 目錄的提交信息抽出爲新的 branchcode
cd {path/to/project_root} git subtree split -P {component_a 的相對路徑} -b {新分支名稱}
二、新建 component_a 倉庫,並從源倉庫的新分支中拉內容component
mkdir ~/component_a cd ~/component_a git init git pull {path/to/project_root} {新分支名稱}