使用git下載專案下的單個檔案或目錄
阿新 • • 發佈:2018-11-10
因為svn是基於檔案儲存的,所以用svn可以方便地下載專案下的單個檔案或目錄,但是git是基於元資料儲存的,因此操作會麻煩一些,要用到git的sparse checkout模式。步驟如下:
開啟git bash,cd到本地專案目錄(LocalPro/),建立一個空的本地倉庫,把遠端倉庫的url(remoteURL)加入到config檔案中去:
cd LocalProj
git init
git remote add -f origin <remoteURL>
在config中設定sparse checkout模式為true:
git config core.sparseCheckout true
把需要checkout(即需要下載的)檔案或目錄寫入.git/info/sparse-checkout
echo fileOrdir >> .git/info/sparse-checkout
然後下載
git pull origin master
以一個例子完整說明整個過程。
假設之前已經通過git clone的方式把專案https://github.com/Yourens/decaf_PA2_2018完整地克隆到本地目錄d:\PA2,之後遠端倉庫更新了https://github.com/Yourens/decaf_PA2_2018/tree/master/TestCases的內容,但是本地專案已經做了修改,不能重新git clone整個遠端倉庫,這時就需要單獨clone遠端的TestCases目錄。git bash命令為
cd /d/PA2
git init
git remote add -f origin https://github.com/Yourens/decaf_PA2_2018.git
git config core.sparseCheckout true
echo 'TestCases' >> .git/info/sparse-checkout
git pull origin master