🧽

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

2024/04/13に公開

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

shallow clone、partial cloneの活用

git clone --depth 1で取得したレポジトリには制約がある。

git clone --depth 1 <URL>

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

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

過去履歴がほしいとき。

git fetch --depth 100

partial cloneの活用

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

shallow clone解除(全履歴)

全履歴取得なので、ストレージの負担は大きい。

git fetch --unshallow

タグ情報が欲しい時

git fetch --tags

git describe --tagsでタグが見つからず、表示されない場合

git fetch --filter=tree:0 --no-recurse-submodules --unshallow
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の利用は、ちょっと注意が必要みたい。

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

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

git clone --mirror URL

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

  1. mirrorを--depth 1でcloneする。
  2. cloneしたフォルダに移り、--filter=tree:0--unshallowを指定してfetchする。
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コマンドで作業コピーが失敗する。

更新がうまくいかない場合に、試すこと。

git remote update --prune
git fetch --depth 1 --refetch --filter=tree:0
git fetch --unshallow --refetch --filter=tree:0

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