Git/Shallow
< Git
Shallow Clone
git clone --filter=blob:none <url> creates a blobless clone. ... git clone --filter=tree:0 <url> creates a treeless clone. ... git clone --depth=1 <url> creates a shallow clone.
git clone --depth=1 <url> -b <branch> # clone at branch
References:
- Get up to speed with partial clone and shallow clone - The GitHub Blog - https://github.blog/open-source/git/get-up-to-speed-with-partial-clone-and-shallow-clone/
Unshallow Clone
To Unshallow a Shallow Clone
git clone <url> --depth 1 git fetch --unshallow
vs
git clone <url>
Incrementally:
git clone <url> --depth 1
git fetch --deepen git fetch --deepen ... # or git fetch --depth 3 git fetch --depth 6 ..
git fetch --unshallow
Reference:
- git "shallow clone + unshallow" vs "normal clone" - Stack Overflow - https://stackoverflow.com/questions/67838180/git-shallow-clone-unshallow-vs-normal-clone
NOTE: Eventually, the final --unshallow gets you everything you would have gotten, had you been able to do a full clone all at once without error. Note that you may want to use --no-single-branch during the initial shallow clone, or fix up the fetch refspec after the initial shallow clone.
Convert Cloned Repo to Shallow
git clone --depth-1 <repo> -b <branch> git clone --depth 1 org-1234@github.com:MYORG/MYREPO.git -b MYREPO/MYBRANCH MYBRANCH git clean -x -f -d . git pull --depth 1 git fetch --depth 1 git reflog expire --expire=0 git reflog expire --expire=now --all git show-ref -s HEAD git show-ref -s HEAD > .git/shallow git prune git prune-packed git repack git pull --shallow-since=1999-01-01 git pull --shallow-since=2026-03-19 git gc git gc --prune=all git gc --prune=now git gc --aggressive git tag -d $(git tag -l) git fsck --full --no-reflogs # fix: # $ git gc # warning: reflog of 'HEAD' references pruned commits git reflog expire --all --stale-fix git gc du --si --max-depth=1
References:
- Converting git repository to shallow? - Stack Overflow - https://stackoverflow.com/questions/4698759/converting-git-repository-to-shallow
UPDATED BRUTE FORCE CONVERSION METHOD
git config remote.origin.fetch "+refs/heads/MYREPO/MYBRANCH:refs/remotes/origin/MYREPO/MYBRANCH"
rm -rf .git/objects/*
## optional cleanup: rm -f .git/info/refs
## optional cleanup: rm -f .git/{COMMIT_EDITMSG,}
# optional: rm .git/hooks/*
# optional: rm -rf .git/lfs
# optional: rm -rf .git/refs/*
# optional: rm -rf .git/logs/*
rm .git/packed-refs
git reflog expire --all --stale-fix
git fetch
git checkout MYREPO/MYBRANCH
git pull
git gc
du --si --max-depth=1
BRUTE FORCE CONVERSION METHOD
cd .git # delete top level files --- # dir: branches/ hooks/ info/ lfs/ logs/ objects/ refs/ # files: COMMIT_EDITMSG config description FETCH_HEAD HEAD index ORIG_HEAD packed-refs shallow # removes extras: COMMIT_EDITMSG, FETCH_HEAD # ignore unused: description rm -f * # remove lfs/ rm -rf lfs/ # clear logs/ and sub-dirs rm -rf logs/* # clear hooks/ rm hooks/* # clear write-projected objects/pack #rm -f objects/pack/* #rm objects/info/packs rm -rf objects/* # remove refs rm -rf refs/* rm info/refs rm COMMIT_EDITMSG rm packed-refs # will be recreated: rm FETCH_HEAD # make origin head shallow cat ORIG_HEAD > shallow # fix config: # [origin] # replace: fetch = +refs/heads/*:refs/remotes/origin/* # with: fetch = +refs/heads/MYREPO/MYBRANCH:refs/remotes/origin/MYREPO/MYBRANCH git config --get remote.origin.fetch +refs/heads/*:refs/remotes/origin/* # git config remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*" ## Limit fetch to just the one branch: git config remote.origin.fetch "+refs/heads/MYREPO/MYBRANCH:refs/remotes/origin/MYREPO/MYBRANCH" git config --get remote.origin.fetch +refs/heads/MYREPO/MYBRANCH:refs/remotes/origin/MYREPO/MYBRANCH # copy shallow copies .git/* -- config, description, FETCH_HEAD, HEAD, ORIG_HEAD, packed-refs, shallow # extras: cp ../../shallow/.git/* ./ cd .. # Fetch - should only fetch the current branch only git fetch