How to clone with depth in Git
The –depth parameter controls how many commits to download during clone, providing flexible balance between history access and clone performance. As the creator of CoreUI, a widely used open-source UI library, I’ve optimized Git operations for various use cases throughout my 25 years of development experience. The most versatile approach is choosing appropriate depth value based on requirements: depth 1 for CI/CD, deeper values for development with recent history. This method enables customizable clone performance, preserves recent history for debugging, and allows incremental history deepening when needed.
Use git clone –depth with specific number to download limited commit history balancing performance and history access.
# Depth 1 - only latest commit (fastest)
git clone --depth 1 https://github.com/user/repo.git
# Downloads: 1 commit
# Use case: CI/CD, quick builds
# Depth 10 - last 10 commits
git clone --depth 10 https://github.com/user/repo.git
# Downloads: 10 commits
# Use case: Recent history for debugging
# Depth 50 - last 50 commits
git clone --depth 50 https://github.com/user/repo.git
# Downloads: 50 commits
# Use case: Code review with history context
# Depth 100 - last 100 commits
git clone --depth 100 https://github.com/user/repo.git
# Downloads: 100 commits
# Use case: Development with substantial history
Comparing different depth values:
# Example: Large repository (React)
# Depth 1
git clone --depth 1 https://github.com/facebook/react.git
# Size: ~15MB, Time: 3s, Commits: 1
# Depth 10
git clone --depth 10 https://github.com/facebook/react.git
# Size: ~18MB, Time: 4s, Commits: 10
# Depth 50
git clone --depth 50 https://github.com/facebook/react.git
# Size: ~30MB, Time: 7s, Commits: 50
# Depth 100
git clone --depth 100 https://github.com/facebook/react.git
# Size: ~45MB, Time: 12s, Commits: 100
# Full clone (no depth)
git clone https://github.com/facebook/react.git
# Size: ~270MB, Time: 30s, Commits: ~20,000
Depth with branch specification:
# Clone specific branch with depth
git clone --depth 10 --branch develop https://github.com/user/repo.git
# Downloads last 10 commits from develop branch
# Clone multiple branches with depth
git clone --depth 10 https://github.com/user/repo.git
cd repo
git remote set-branches origin 'develop'
git fetch --depth 10 origin develop
# Now have main and develop with 10 commits each
# Clone tag with depth
git clone --depth 1 --branch v2.0.0 https://github.com/user/repo.git
# Downloads commit at tag v2.0.0 only
Deepening existing shallow clones:
# Clone with depth 10
git clone --depth 10 https://github.com/user/repo.git
cd repo
# Check current depth
git log --oneline
# Shows 10 commits
# Deepen by 20 more commits
git fetch --deepen 20
git log --oneline
# Now shows 30 commits
# Deepen to specific depth
git fetch --depth 50
git log --oneline
# Now shows 50 commits
# Get full history
git fetch --unshallow
git log --oneline
# Now shows all commits
Practical depth selection guide:
# Depth 1: Absolute minimum
# - CI/CD pipelines
# - Docker image builds
# - Quick code download
# - Deployment scripts
git clone --depth 1 https://github.com/user/repo.git
# Depth 10-20: Light development
# - Quick bug fixes
# - Recent history review
# - Lightweight development
git clone --depth 10 https://github.com/user/repo.git
# Depth 50-100: Standard development
# - Feature development
# - Code archaeology
# - Blame analysis
git clone --depth 50 https://github.com/user/repo.git
# Full clone: Complete history
# - Maintainer work
# - Historical analysis
# - Repository migration
git clone https://github.com/user/repo.git
Using depth in automation:
# CI/CD script with depth 1
#!/bin/bash
BRANCH=${1:-main}
REPO_URL=${2:-https://github.com/user/repo.git}
git clone --depth 1 --single-branch --branch $BRANCH $REPO_URL
cd repo
npm install
npm test
npm run build
# Development script with depth 50
#!/bin/bash
BRANCH=${1:-main}
REPO_URL=${2:-https://github.com/user/repo.git}
git clone --depth 50 --branch $BRANCH $REPO_URL
cd repo
npm install
code .
# Deployment script with specific tag depth
#!/bin/bash
TAG=${1:-v1.0.0}
REPO_URL=${2:-https://github.com/user/repo.git}
git clone --depth 1 --branch $TAG $REPO_URL deploy
cd deploy
npm install
npm run build
npm run deploy
Depth in different scenarios:
# Scenario 1: Fast CI build
git clone --depth 1 --single-branch https://github.com/user/repo.git
# Minimal download, fastest build
# Scenario 2: Code review with context
git clone --depth 20 --branch feature-x https://github.com/user/repo.git
# Enough history to see recent changes
# Scenario 3: Bug investigation
git clone --depth 100 https://github.com/user/repo.git
# Substantial history for debugging
# Scenario 4: Fork and contribute
git clone https://github.com/user/repo.git
# Full history for proper contribution
# Scenario 5: Mirror repository
git clone --mirror https://github.com/user/repo.git
# Complete mirror including all refs
Depth with Git operations:
# Clone with depth
git clone --depth 10 https://github.com/user/repo.git
cd repo
# Limited operations on shallow clone
git log
# Shows only 10 commits
git blame file.js
# Shows blame for available history only
git bisect
# Limited bisect range (requires more history)
git fetch --unshallow # Get full history first
# Push works normally
git add .
git commit -m "Changes"
git push
# Pushing works fine with shallow clone
# Pull works normally
git pull
# Fetches and merges latest changes
Optimizing network usage with depth:
# Scenario: Slow network connection
# Full clone would take 30 minutes
# Shallow clone takes 2 minutes
git clone --depth 1 https://github.com/large/repository.git
# Later, if you need more history:
git fetch --deepen 10
# Incrementally download more commits
# Or get everything:
git fetch --unshallow
# Complete the repository when network is faster
Configuration for automated tools:
# GitHub Actions with depth
name: Build
on: [push]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 10 # Last 10 commits
# GitLab CI with depth
variables:
GIT_DEPTH: 50 # Last 50 commits
# CircleCI with depth
version: 2.1
jobs:
build:
steps:
- checkout:
depth: 1 # Shallow clone
# Jenkins with depth
stage('Checkout') {
steps {
git url: 'https://github.com/user/repo.git',
depth: 1,
branch: 'main'
}
}
Monitoring clone performance:
# Time different depth values
echo "Testing depth 1:"
time git clone --depth 1 https://github.com/facebook/react.git react-d1
du -sh react-d1
echo "Testing depth 10:"
time git clone --depth 10 https://github.com/facebook/react.git react-d10
du -sh react-d10
echo "Testing depth 50:"
time git clone --depth 50 https://github.com/facebook/react.git react-d50
du -sh react-d50
echo "Testing full clone:"
time git clone https://github.com/facebook/react.git react-full
du -sh react-full
# Example output:
# depth 1: 3s, 15MB
# depth 10: 4s, 18MB
# depth 50: 7s, 30MB
# full: 30s, 270MB
Working with depth in team environment:
# Documentation for team
# .github/CONTRIBUTING.md
# Quick contribution (depth 10)
git clone --depth 10 https://github.com/org/repo.git
# Full development (complete history)
git clone https://github.com/org/repo.git
# CI/CD (depth 1)
git clone --depth 1 --single-branch https://github.com/org/repo.git
# Converting shallow to full if needed
git fetch --unshallow
Here the –depth parameter specifies exact number of commits to download from repository tip. Lower depth values provide faster clones with less disk space but limited history. Higher depth values balance performance with history access for debugging and blame operations. The git fetch –deepen incrementally adds commits to shallow repository. The git fetch –unshallow converts shallow clone to complete repository. Depth 1 optimizes CI/CD pipelines while depth 50-100 suits development workflows. Different scenarios require different depth values balancing speed and functionality.
Best Practice Note:
This is the depth-based cloning strategy we use across CoreUI projects to optimize clone performance while maintaining necessary history access for different workflows. Choose depth 1 for automated pipelines where only current code matters, use depth 10-50 for development work requiring recent history context, leverage git fetch –deepen to incrementally add history only when needed for specific operations, and document recommended depth values for your team based on repository size and typical workflow requirements.



