特定ディレクトリのみをgit cloneする方法の備忘録です。
はじめに
大きなレポジトリから特定のディレクトリのみをローカルにクローンしたいことがありました。
やり方がパッとわからなかったので、やり方のメモを残します。
特定ディレクトリのみをgit cloneする方法
コマンド
下記のようなコマンドで、特定ディレクトリのみをgit cloneできます。
git clone --filter=blob:none --sparse https://github.com/user/repo.git cd repo git sparse-checkout set path/to/directory
- 一行目:
git clone --filter=blob:none --sparse https://...
まずこの一行目では、--filter=blob:noneで実ファイルをダウンロードしない状態から始め、--sparseでsparse-checkoutを有効にします。
sparse-checkout とは、リポジトリの作業ツリー(実際にチェックアウトされる部分)を一部だけに絞るGit の機能です(sparceは"まばら"の意)。 この時点ではルート直下のファイルのみ展開した状態で開始されます。
- 二行目:
cd repo
これは一行目でクローンしてきた空のレポジトリ(ルート直下のファイルのみ展開されている)にディレクトリ移動しているだけですね。
- 三行目:
git sparse-checkout set path/to/directory
setでチェックアウト対象のディレクトリパスを指定し、sparse-checkoutで指定のディレクトリのみをチェックアウトします。
以上、これらのGitコマンドで特定ディレクトリのみをgit cloneすることが可能です。
それでは実際にやってみます。
具体例
Google CloudのTerraformモジュール集から、特定ディレクトリmodules/cloud-run-v2のみをクローンしてみます。
まずは、普通にクローンするようにルートの.gitを指定した上で--filter=blob:none --sparseを付与して実行します。
git clone --filter=blob:none --sparse https://github.com/GoogleCloudPlatform/cloud-foundation-fabric.git
次にレポジトリのディレクトリに移動します。
cd cloud-foundation-fabric
この時点のファイルを確認してみると、ルート直下のファイルのみが取得されていることがわかります。
$ ls -la total 1632 drwxr-xr-x 23 bioerrorlog staff 736 2 23 22:07 . drwxr-xr-x 93 bioerrorlog staff 2976 2 23 22:07 .. -rw-r--r-- 1 bioerrorlog staff 53 2 23 22:07 .codespellrc drwxr-xr-x 13 bioerrorlog staff 416 2 23 22:07 .git -rw-r--r-- 1 bioerrorlog staff 85 2 23 22:07 .gitattributes -rw-r--r-- 1 bioerrorlog staff 556 2 23 22:07 .gitignore -rw-r--r-- 1 bioerrorlog staff 4090 2 23 22:07 .pre-commit-config.yaml -rw-r--r-- 1 bioerrorlog staff 78 2 23 22:07 .style.yapf -rw-r--r-- 1 bioerrorlog staff 182 2 23 22:07 .tflint.hcl -rw-r--r-- 1 bioerrorlog staff 560 2 23 22:07 .yamllint lrwxr-xr-x 1 bioerrorlog staff 9 2 23 22:07 AGENTS.md -> GEMINI.md -rw-r--r-- 1 bioerrorlog staff 311444 2 23 22:07 CHANGELOG.0.md -rw-r--r-- 1 bioerrorlog staff 340612 2 23 22:07 CHANGELOG.md -rw-r--r-- 1 bioerrorlog staff 63422 2 23 22:07 CONTRIBUTING.md -rw-r--r-- 1 bioerrorlog staff 10981 2 23 22:07 CURSED_KNOWLEDGE.md -rw-r--r-- 1 bioerrorlog staff 7815 2 23 22:07 FABRIC-AND-CFT.md -rw-r--r-- 1 bioerrorlog staff 3533 2 23 22:07 GEMINI.md -rw-r--r-- 1 bioerrorlog staff 11357 2 23 22:07 LICENSE -rw-r--r-- 1 bioerrorlog staff 6487 2 23 22:07 README.md -rw-r--r-- 1 bioerrorlog staff 3568 2 23 22:07 REFERENCES.md -rw-r--r-- 1 bioerrorlog staff 1110 2 23 22:07 default-versions.tf -rw-r--r-- 1 bioerrorlog staff 1114 2 23 22:07 default-versions.tofu -rw-r--r-- 1 bioerrorlog staff 24436 2 23 22:07 diagram.svg
最後に、今回の目的のディレクトリパスmodules/cloud-run-v2を指定して、sparse-checkoutします。
git sparse-checkout set modules/cloud-run-v2
これで指定のディレクトリが取得できました。
確認します。
$ ll total 1632 drwxr-xr-x 24 bioerrorlog staff 768 2 23 22:13 ./ drwxr-xr-x 93 bioerrorlog staff 2976 2 23 22:07 ../ -rw-r--r-- 1 bioerrorlog staff 53 2 23 22:07 .codespellrc drwxr-xr-x 13 bioerrorlog staff 416 2 23 22:13 .git/ -rw-r--r-- 1 bioerrorlog staff 85 2 23 22:07 .gitattributes -rw-r--r-- 1 bioerrorlog staff 556 2 23 22:07 .gitignore -rw-r--r-- 1 bioerrorlog staff 4090 2 23 22:07 .pre-commit-config.yaml -rw-r--r-- 1 bioerrorlog staff 78 2 23 22:07 .style.yapf -rw-r--r-- 1 bioerrorlog staff 182 2 23 22:07 .tflint.hcl -rw-r--r-- 1 bioerrorlog staff 560 2 23 22:07 .yamllint lrwxr-xr-x 1 bioerrorlog staff 9 2 23 22:07 AGENTS.md@ -> GEMINI.md -rw-r--r-- 1 bioerrorlog staff 311444 2 23 22:07 CHANGELOG.0.md -rw-r--r-- 1 bioerrorlog staff 340612 2 23 22:07 CHANGELOG.md -rw-r--r-- 1 bioerrorlog staff 63422 2 23 22:07 CONTRIBUTING.md -rw-r--r-- 1 bioerrorlog staff 10981 2 23 22:07 CURSED_KNOWLEDGE.md -rw-r--r-- 1 bioerrorlog staff 7815 2 23 22:07 FABRIC-AND-CFT.md -rw-r--r-- 1 bioerrorlog staff 3533 2 23 22:07 GEMINI.md -rw-r--r-- 1 bioerrorlog staff 11357 2 23 22:07 LICENSE -rw-r--r-- 1 bioerrorlog staff 6487 2 23 22:07 README.md -rw-r--r-- 1 bioerrorlog staff 3568 2 23 22:07 REFERENCES.md -rw-r--r-- 1 bioerrorlog staff 1110 2 23 22:07 default-versions.tf -rw-r--r-- 1 bioerrorlog staff 1114 2 23 22:07 default-versions.tofu -rw-r--r-- 1 bioerrorlog staff 24436 2 23 22:07 diagram.svg drwxr-xr-x 4 bioerrorlog staff 128 2 23 22:13 modules/ # modulesが追加でダウンロードされた $ ls modules/ README.md cloud-run-v2 # modulesディレクトリには、指定のcloud-run-v2のみ(+ディレクトリ直下のファイル)がダウンロードされた
おわりに
特定ディレクトリのみをgit cloneする方法の簡単な備忘録でした。
以上!
[関連記事]