Try   HackMD

Git 節省空間相關的解決方案

tags: git

Single Branch & Depth

因為不喜歡把太多東西放到筆電裡,常常想用筆電的時候只 clone repo 中需要的那部分就好,所以查到了可以只 clone 一個 branch 的方法:--branch

​​​​git clone -b branch repo@git.service.provider

同時,也常常不需要完整的 commit history,這時候可以用 --depth 參數指定需要的 commit history 個數。比如

​​​​git clone -b branch --depth 10 repo@git.service.provider

但是用 -b 指定 branch 的方式 clone 下來,其實表示的是整個 remote 只當成有這個 branch,之後再 fetch 也只能得到這個 branch。而 --depth 的部分沒有影響,每次 fetch 可以指定不同的 depth,不會從此就得不到更久以前的 commit。

所以,如果之後需要 push/fetch 其他的 branch,就要利用 set-branches 先把這個新的 branch 加到這個 remote 的 branch list 裡。
[git-remote#set-branches]

​​​​git remote set-branches --add origin another-branch

之後再 fetch 就可以看到這個新的 branch 啦!

​​​​> git fetch
​​​​> git branch -r
​​​​origin/branch
​​​​origin/another-branch

Sparse Checkout

如果想要只 fetch 某一個 folder,可以使用 sparseCheckout:

git clone --no-checkout --depth 1 --single-branch git@foo/bar.git
cd bar
git config core.sparseCheckout true
echo "/mySubfoler/"> .git/info/sparse-checkout
git checkout master