
如果程式專案中有非常多次的提交(commit),我們可以在使用 git
命令時,帶上 --depth
參數來進行淺克隆(shallow clone),使其只 clone 到特定深度的歷史提交:
# 僅 clone 指定深度提交
$ git clone --depth [depth] [origin]
上述的做法適用於已知提交的深度,亦即某次提交距離最新的提交有幾步。當然,我們也可以先進行一次淺克隆(shallow clone)之後,再切到想要的提交:
# 僅 clone 指定深度提交
$ git clone --depth [depth] [origin]
# 獲取想要的提交
$ cd [project_directory]
$ git fetch --depth=1 origin [commit_hash]
$ git checkout [commit_hash]
除此之外,也可以結合 如何使用 git 命令 clone 程式專案的特地分枝(branch)? 中提到的 --single-branch
參數,搭配 --depth
參數使用來僅 clone 專案指定分枝的特定提交深度:
# 僅 clone 指定分枝的特定提交深度
$ git clone --single-branch --branch [branch] --depth 1 [origin]
張貼留言