How to bundle a repository in Git
Git bundle creates a single file containing all repository data including history, branches, and tags, allowing offline repository transfer when network access is unavailable. As the creator of CoreUI with 26 years of development experience, I’ve used Git bundles to transfer repositories across air-gapped networks, backup critical projects, and share full repository history via email or USB drives.
The most reliable approach uses git bundle create with specific refs or complete repository bundling.
Create Complete Repository Bundle
# Bundle entire repository with all branches and tags
git bundle create repo.bundle --all
# Verify bundle
git bundle verify repo.bundle
# Clone from bundle
git clone repo.bundle my-repo
cd my-repo
# Check all branches
git branch -a
# Repository is fully functional with complete history
Bundle Specific Branch
# Bundle only main branch
git bundle create main.bundle main
# Bundle with commit history
git bundle create feature.bundle feature-branch
# Clone from branch bundle
git clone feature.bundle feature-repo
# Verify it's a complete working repository
cd feature-repo
git log --oneline
Bundle Commit Range
# Bundle commits from tag to HEAD
git bundle create recent.bundle v1.0.0..HEAD
# Bundle last 10 commits
git bundle create last10.bundle HEAD~10..HEAD
# Bundle specific commit range
git bundle create range.bundle abc123..def456
# To use range bundle, need base commits in target repo
git pull range.bundle
Bundle Multiple Branches
# Bundle specific branches
git bundle create branches.bundle main develop feature-x
# Bundle all local branches
git bundle create local.bundle --branches
# Bundle all remote branches
git bundle create remote.bundle --remotes
# Clone and check branches
git clone branches.bundle multi-branch
cd multi-branch
git branch -a
Bundle with Tags
# Bundle all tags
git bundle create with-tags.bundle --tags
# Bundle specific tags
git bundle create releases.bundle v1.0.0 v2.0.0 v3.0.0
# Bundle branches and tags
git bundle create complete.bundle --all --tags
# Verify tags after cloning
git clone complete.bundle tagged-repo
cd tagged-repo
git tag
Incremental Bundles
# Create initial bundle
git bundle create initial.bundle --all
# Later, create incremental bundle with new commits
git bundle create update.bundle ^initial.bundle --all
# On receiving end:
# 1. Clone from initial bundle
git clone initial.bundle my-repo
# 2. Apply incremental bundle
cd my-repo
git pull ../update.bundle
# All new commits now available
Bundle Verification
# Verify bundle is valid
git bundle verify repo.bundle
# Output:
# repo.bundle is okay
# List references in bundle
git bundle list-heads repo.bundle
# Output shows all branches and tags:
# abc123 refs/heads/main
# def456 refs/heads/develop
# Verify specific branch exists
git bundle list-heads repo.bundle | grep main
Extract from Bundle Without Cloning
# Fetch from bundle into existing repo
cd existing-repo
git fetch /path/to/repo.bundle refs/heads/main:refs/heads/imported-main
# Now have imported-main branch
git branch
# Pull specific branch
git pull /path/to/repo.bundle main
# Add bundle as remote
git remote add bundle-origin /path/to/repo.bundle
git fetch bundle-origin
Backup Repository with Bundle
# Create timestamped backup
DATE=$(date +%Y%m%d)
git bundle create backup-$DATE.bundle --all
# Verify backup
git bundle verify backup-$DATE.bundle
# Store offsite or on external drive
cp backup-$DATE.bundle /mnt/backup/
# Restore from backup
git clone backup-$DATE.bundle restored-repo
# All history, branches, and tags preserved
Transfer Repository Offline
# On source machine with network
cd my-project
git bundle create my-project.bundle --all
# Copy my-project.bundle to USB drive
# On target air-gapped machine
cd /path/to/usb
git clone my-project.bundle my-project
# Full repository now available offline
cd my-project
git log
git branch -a
Bundle for Email Transfer
# Create bundle
git bundle create feature.bundle feature-branch
# Check size
ls -lh feature.bundle
# If too large, split bundle
split -b 10M feature.bundle feature.bundle.part
# Attach parts to email
# feature.bundle.partaa
# feature.bundle.partab
# feature.bundle.partac
# On receiving end, reassemble
cat feature.bundle.part* > feature.bundle
# Verify and clone
git bundle verify feature.bundle
git clone feature.bundle feature
Bundle with Compression
# Create bundle with maximum compression
git bundle create repo.bundle --all
# Bundle is already compressed internally
# For additional compression
gzip repo.bundle
# Creates repo.bundle.gz
# To use compressed bundle
gunzip repo.bundle.gz
git bundle verify repo.bundle
git clone repo.bundle my-repo
# Alternatively, pipe directly
git bundle create - --all | gzip > repo.bundle.gz
gunzip -c repo.bundle.gz | git clone - my-repo
Automated Bundle Backups
#!/bin/bash
# backup-repo.sh
REPO_PATH="/path/to/repo"
BACKUP_DIR="/path/to/backups"
DATE=$(date +%Y%m%d-%H%M%S)
BUNDLE_NAME="backup-$DATE.bundle"
cd "$REPO_PATH"
# Create bundle
git bundle create "$BACKUP_DIR/$BUNDLE_NAME" --all
# Verify bundle
if git bundle verify "$BACKUP_DIR/$BUNDLE_NAME"; then
echo "✓ Backup created: $BUNDLE_NAME"
# Delete backups older than 30 days
find "$BACKUP_DIR" -name "backup-*.bundle" -mtime +30 -delete
echo "✓ Old backups cleaned"
else
echo "✗ Bundle verification failed"
exit 1
fi
# Make executable
chmod +x backup-repo.sh
# Add to cron for daily backups
crontab -e
# 0 2 * * * /path/to/backup-repo.sh
Bundle vs Archive
# git archive creates snapshot (no history)
git archive --format=zip --output=snapshot.zip HEAD
# git bundle includes full history
git bundle create repo.bundle --all
# Archive is smaller but loses history
ls -lh snapshot.zip repo.bundle
# Bundle preserves all commits
git clone repo.bundle restored
cd restored
git log --oneline # Full history available
Best Practice Note
This is the bundle strategy we use across all CoreUI repositories for offline transfers and backups. Git bundles package complete repositories into single files, preserving all history, branches, and tags for offline transfer. Always verify bundles after creation, use –all flag for complete backups, create incremental bundles for updates, and compress bundles for storage efficiency. Bundles work perfectly for air-gapped environments, email transfers, and external backup drives. Unlike archives, bundles maintain full Git functionality including ability to push/pull changes.
Related Articles
For related Git operations, check out how to archive repository in Git and how to backup Git repository.



