🧽

Gitでなるべくストレージ容量を節約したい

に公開

Gitでストレージをなるべく節約する。

shallow cloneの場合

git clone --depth 1

git clone --depth 1 <URL>

複数のブランチとタグ情報をclone[1]

git clone --depth 1 --no-single-branch <URL>

過去履歴がほしいとき。

git fetch --depth 100

タグ情報が欲しい時

git fetch --tags

メインブランチ以外にcheckoutしたい場合

SUB_BRANCHにチェックアウトする場合

git fetch origin SUB_BRANCH:SUB_BRANCH
git checkout SUB_BRANCH
git push -u origin SUB_BRANCH

shallow clone解除(全履歴)

全履歴取得なので、時間もかかるし、ストレージの負担は大きい。

git fetch --unshallow

shallow cloneを解除して、partial cloneのfilterを用いる場合

git describe --tagsでタグが見つからず、表示されない場合にも使える。

git fetch --filter=tree:0 --no-recurse-submodules --unshallow

or

git fetch --filter=blob:none --no-recurse-submodules --unshallow
git describe --tags

filterを指定しておくと、unshallowで、すべてを取得せず、最低限のみ取得してくれているっぽい。
パーシャルクローン(partial clone)というらしい。[2][3]

--filter=tree:0
--filter=blob:none

mirrorレポジトリでsubmoduleを扱う場合には、partial cloneの利用は、ちょっと注意が必要みたい。

partial cloneで最初からcloneする場合

clone時にfilterを指定し、partial cloneをする。

git clone --filter=blob:none <URL> <REPOSITORY>

or

git clone --filter=tree:0 <URL> <REPOSITORY>

ローカルでcloneできるかチェック。

git clone <REPOSITORY> <CLONE_TEST>
cd <CLONE_TEST>
git describe --tags
cd ..
rm -rf <CLONE_TEST>

partial clone & mirror

clone後、git worktreeコマンドで、checkoutすることで、objectsデータをfetch(ダウンロード)しておく。その後、removeで作業ツリーを削除して、容量を節約する。

git clone --mirror --filter=tree:0 <URL> <REPOSITORY>
cd <REPOSITORY>
git worktree add tmp
git worktree remove tmp
git branch -d tmp
cd ..

ローカルでcloneできるかチェック。

git clone <REPOSITORY> <CLONE_TEST>
cd <CLONE_TEST>
git describe --tags
cd ..
rm -rf <CLONE_TEST>

Arch、ManjaroでPKGBUILDのgitソースのストレージ節約

shallow clone & partial clone

PKGBUILDにgit+httpsなどで、gitレポジトリがソースに指定されている場合、makepkgコマンドで作られるmirrorフォルダは下記gitコマンドと同じ形式で作成される模様。

git clone --mirror URL

容量を節約しつつ、makepkgの作業レポジトリ作成の時間も増やさない手順。

  1. mirrorを--depth 1でcloneする。(shallow clone)
  2. cloneしたフォルダに移り、--filter=tree:0--unshallowを指定してfetchする。(shallow解除&partial)
git clone --mirror --depth 1 <URL> <REPOSITORY>
cd <REPOSITORY>
git fetch --filter=tree:0 --no-recurse-submodules --unshallow
cd ..

1の手順のみだと、--depth 1を指定しないcloneと比べて、作業レポジトリのコピーに時間がかかる。
2の手順を行うと、その時間が短縮される。
またclone時にfilterを指定すると、makepkgコマンドで作業コピーが失敗する。必要なobjectsファイルがダウンロードされていないためで、worktreeコマンドなどを利用して、あらかじめfetchしておくことで対処できる。その方法は、次の項目を参照してください。

また以下のコマンドでcloneすると、tag情報の同期がされないので、別途オプション指定が必要そうです。
(tag情報などでpkgverが更新される場合もあるので、tag情報なども必要になってきます。)

git clone --bare --depth 1 <URL> <REPOSITORY>
git clone --bare --config remote.origin.mirror=true --depth 1 <URL> <REPOSITORY>

partial clone

最初にcloneする時点で、filterを指定してpartial cloneを行う場合。
clone後、git worktreeコマンドで、checkoutしておき、objectsデータを一通りfetch(ダウンロード)しておく。容量節約のために、その後、worktreeをremoveしておく。この手順を追加することで、makepkgコマンドで作業コピーが失敗するのを回避できる。mirrorレポジトリなので、checkoutコマンドなどは使えないが、worktreeコマンドなどは利用できる。

git clone --mirror --filter=tree:0 <URL> <REPOSITORY>

or

git clone --mirror --filter=blobe:none <URL> <REPOSITORY>
cd <REPOSITORY>
git worktree add tmp
git worktree remove tmp
git branch -d tmp
cd ..

ローカルでcloneできるか、tag情報が取得できているかなどをチェックする。

git clone <REPOSITORY> <CLONE_TEST>
cd <CLONE_TEST>
git describe --tags
cd ..
rm -rf <CLONE_TEST>

scalarを用いる

--full-cloneを指定しておくと、活用できそうです。

scalar clone -b <BRANCH> --no-src --full-clone <URL> <REPOSITORY_TMP>
cd <REPOSITORY_TMP>
git config --bool core.bare true
git config fetch.showForcedUpdates true
cd ..
mkdir <REPOSITORY>
mv <REPOSITORY_TMP>/.git/* <REPOSITORY>
scalar unregister <REPOSITORY_TMP>
rm -rf <REPOSITORY_TMP>

github fork元のタグを取得

upstreamからタグ情報を取得する手順例

git remote add upstream <fork元のリポジトリ>
git fetch upstream --filter=tree:0 --no-recurse-submodules --unshallow
git describe --tags upstream/master
git describe --tags origin/master
git push origin --tags

参考サイト

Git の最新アップデートから考える開発手法の潮流
パーシャルクローンとシャロークローンを活用しよう[3:1]
git リポジトリを clone 後に軽量化する (blobless化)[2:1]
以前forkしたgithubリポジトリを追従させる方法[4]
Github で fork したリポジトリの簡便な同期と運用[5]
Git で shallow clone するときに全ブランチの最新履歴を取得する[1:1]

脚注
  1. https://zenn.dev/tetsu_koba/articles/77347ef2c283ac ↩︎ ↩︎

  2. https://qiita.com/irgaly/items/0aace5cbd44aa4220733 ↩︎ ↩︎

  3. https://github.blog/jp/2021-01-13-get-up-to-speed-with-partial-clone-and-shallow-clone/ ↩︎ ↩︎

  4. https://zenn.dev/tetsu_koba/articles/77347ef2c283ac ↩︎

  5. https://qiita.com/jsasaki/items/f77c23f4fea3f7edb812 ↩︎

Discussion