0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

git で必要な branch だけ clone / fetch する

0
Posted at

はじめに

大きな git リポジトリを clone しようとすると,ダウンロードに時間がかかり,場合によっては「fatal: git upload-pack: aborting due to possible repository corruption on the remote side.」で clone に失敗する.GitHubブログでは,ブロブレスクローン git clone --filter=blob:none <url> やツリーレスクローン git clone --filter=tree:0 <url> が紹介されているが,これらでは十分小さくならない場合がある.かといって,シャロークローンにしてしまうと,必要なデータが fetch できない.私の場合に必要だったのは,シングルブランチクローンだった.

各論

シングルブランチクローン

以下のコマンドで,特定のブランチだけ clone できる.-b だけ紹介している記事があるが,--single-branch も入れないと全部落としてくるので意味がない.

$ git clone -b <branch> --single-branch <url>

ブランチの追加

--single-branch で clone したうえで,必要なブランチを追加で fetch したいときは,次のようにすればよい.

$ git remote set-branches --add origin <branch>
$ git fetch

リモートブランチの一覧

--single-branch で clone した場合,他のブランチは git branch -r では見えない.set-branhces --add したいブランチ名を探すために,リモートブランチ名の一覧を得たい場合は,以下のコマンドで取得できる.

$ git ls-remote --heads origin

サーバー側の config

サーバー側の設定で,大きなリポジトリの clone 失敗を抑止できる場合がある.

git config pack.windowMemory 4g
git config pack.threads 1
git config pack.window 0

おわりに

今回は,git で,特定のブランチだけクローンする方法を紹介した.これにより,クローンにかかる時間と,ローカルリポジトリのディスク消費量を削減できる.

参考文献の1つ目では,--depthを調整してさらにフェッチ量を削減したり,特定のタグだけフェッチする方法も紹介されているので,必要に応じて参照されたい.

参考文献

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?