Today we will explain how to save space on cloned git repos with shallow repos. This will achieve a lower size of the repo if you do not need all repo history.
Step 1: Clone a repository shallowly (depth = 1)
git clone --depth 1 https://github.com/your-org/your-repo.git
--depth 1
tells Git to fetch only the tip of the default branch (usuallymain
ormaster
).- You can also specify a non‑default branch:
git clone --depth 1 --branch develop https://github.com/your-org/your-repo.git
The resulting .git
directory will contain just enough objects to represent the latest snapshot, typically a few megabytes even for large projects.
Step 2: Keep the shallow clone up‑to‑date while preserving the depth
When you need to pull newer changes, you must fetch again with the same depth. A normal git pull
would try to download the full history, which defeats the purpose. Use the following pattern:
# Fetch the newest commit(s) from the remote, still limited to depth 1
git fetch --depth 1 origin <branch>
# Reset the working tree to match the fetched tip
git reset --hard origin/<branch>
Replace <branch>
with the name of the branch you’re tracking (e.g., main
). In a CI pipeline you can combine these into a single line:
git fetch --depth 1 origin main && git reset --hard origin/main
Why this works:
git fetch --depth 1
adds only the latest commit from the remote, discarding any older objects that might have been cached.git reset --hard
moves your local branch pointer to the newly fetched commit and updates the working tree accordingly.
If you ever need to fetch more history (e.g., for debugging), you can deepen the clone later:
git fetch --deepen 10 # adds the last 10 commits
But for pure space‑saving CI runs, keep the depth at 1 each time.
Bonus: Convert an existing full clone into a shallow one (depth = 1)
If you already have a complete repository on the CI agent and want to shrink it without re‑cloning, you can re‑initialize the shallow state:
# Ensure you are on the branch you want to shallow
git checkout main
# Discard all history except the latest commit
git fetch --depth 1 origin main && git reset --hard origin/main
# Optionally prune any dangling objects to free space
git gc --prune=now --aggressive
Explanation:
git fetch --depth 1 origin main
pulls only the newest commit from the remote, overwriting the local reference toorigin/main
.git reset --hard origin/main
aligns your localmain
branch with that shallow tip.git gc
(garbage collection) removes the now‑unreferenced objects from the repository, reclaiming disk space.
Recap
Goal | Command |
---|---|
Clone shallow (depth 1) | git clone --depth 1 <url> |
Update shallow clone | git fetch --depth 1 origin <branch> && git reset --hard origin/<branch> |
Convert full repo → shallow | git fetch --depth 1 origin <branch> && git reset --hard origin/<branch> && git gc --prune=now --aggressive |
These steps let you keep a CI workspace (or whatever you have) lightweight while still having the latest code ready for building or testing.
Happy coding!