0 / 0
Skip to content

Working with Multiple Branches Using Shallow Clones: A Company Workflow Guide

Posted on August 21, 2025

Hey there!

So, your company has a typical multi-branch setup: main, development, feature/x1, feature/x2, bug branches, and you want to work efficiently with shallow clones? This is a common scenario in company environments, and there are several strategies to handle it without downloading the full history.

Let me show you how to work with multiple branches using shallow clones effectively.

The Company Branch Structure

Let's say your company has this typical setup:

main (production)
├── development (staging)
├── feature/x1 (new feature)
├── feature/x2 (another feature)
└── bug (hotfix branch)

The most efficient approach is to work on one branch at a time:

For Development Work

bash
# Clone the development branch with shallow depth
git clone --depth 1 --single-branch --branch development https://github.com/company/project.git
cd project

# Verify you're on the right branch
git branch -a
# Output: * development

# Work on your feature
git checkout -b feature/my-new-feature
vim src/new-feature.js
git add src/new-feature.js
git commit -m "Add new feature"

# Push to your feature branch
git push origin feature/my-new-feature

For Production Hotfixes

bash
# Clone main branch for production fixes
git clone --depth 1 --single-branch --branch main https://github.com/company/project.git
cd project

# Create hotfix branch
git checkout -b bug/critical-fix
vim src/critical-file.js
git add src/critical-file.js
git commit -m "Fix critical production bug"

# Push hotfix
git push origin bug/critical-fix

For Feature Reviews

bash
# Clone specific feature branch for review
git clone --depth 1 --single-branch --branch feature/x1 https://github.com/company/project.git
cd project

# Review the code
git log --oneline
git diff development..HEAD

Strategy 2: Multiple Shallow Clones (For Parallel Work)

If you need to work on multiple branches simultaneously:

Setup Multiple Directories

bash
# Development work
git clone --depth 1 --single-branch --branch development https://github.com/company/project.git project-dev
cd project-dev

# Production work (in another terminal)
cd ..
git clone --depth 1 --single-branch --branch main https://github.com/company/project.git project-main
cd project-main

# Feature work (in another terminal)
cd ..
git clone --depth 1 --single-branch --branch feature/x1 https://github.com/company/project.git project-feature
cd project-feature

Directory Structure

~/work/
├── project-dev/      # Development branch (200MB)
├── project-main/     # Main branch (200MB)
└── project-feature/  # Feature branch (200MB)
# Total: 600MB instead of 45GB

Strategy 3: Selective Branch Fetching

If you need to switch between branches occasionally:

Initial Clone

bash
# Start with development branch
git clone --depth 1 --single-branch --branch development https://github.com/company/project.git
cd project

Fetch Additional Branches as Needed

bash
# When you need to work on main branch
git fetch --depth 1 origin main
git checkout main

# When you need feature/x1
git fetch --depth 1 origin feature/x1
git checkout feature/x1

# When you need bug branch
git fetch --depth 1 origin bug
git checkout bug

Check What You Have

bash
# See available branches
git branch -a
# Output:
# * development
#   main
#   feature/x1
#   bug

# Check disk usage
du -sh .git
# Output: ~800MB (still much smaller than full clone)

Strategy 4: Component-Based Workflow

For large projects, work on specific components:

Clone Only What You Need

bash
# Clone with sparse checkout
git clone --depth 1 --filter=blob:none --sparse https://github.com/company/project.git
cd project

# Initialize sparse checkout
git sparse-checkout init --cone

# Set specific directories you need
git sparse-checkout set src/frontend
git sparse-checkout set src/backend
git sparse-checkout set docs

# Check what you have
ls -la
# Output: Only the directories you specified

Real Company Workflow Examples

Example 1: Feature Development Cycle

bash
# Day 1: Start new feature
git clone --depth 1 --single-branch --branch development https://github.com/company/project.git
cd project
git checkout -b feature/user-authentication
# Work on feature...

# Day 2: Continue feature
cd project
git pull origin development  # Get latest changes
# Continue working...

# Day 3: Push feature
git push origin feature/user-authentication
# Create pull request from web interface

Example 2: Hotfix Workflow

bash
# Emergency: Production bug found
git clone --depth 1 --single-branch --branch main https://github.com/company/project.git hotfix
cd hotfix

# Create hotfix branch
git checkout -b bug/emergency-fix
vim src/critical-component.js
git add src/critical-component.js
git commit -m "Fix critical production issue"

# Push and create urgent PR
git push origin bug/emergency-fix

Example 3: Code Review Process

bash
# Review feature/x1
git clone --depth 1 --single-branch --branch feature/x1 https://github.com/company/project.git review-feature
cd review-feature

# Review changes
git log --oneline
git diff development..HEAD

# Clean up after review
cd ..
rm -rf review-feature

Advanced Multi-Branch Techniques

Technique 1: Branch-Specific Shallow Clones

bash
# Create a script for different branch workflows
#!/bin/bash
# clone-branch.sh

BRANCH=$1
PROJECT_NAME="company-project"

if [ -z "$BRANCH" ]; then
    echo "Usage: ./clone-branch.sh <branch-name>"
    exit 1
fi

git clone --depth 1 --single-branch --branch $BRANCH https://github.com/company/$PROJECT_NAME.git $PROJECT_NAME-$BRANCH
cd $PROJECT_NAME-$BRANCH
echo "Cloned $BRANCH branch to $PROJECT_NAME-$BRANCH"

Usage:

bash
./clone-branch.sh development
./clone-branch.sh main
./clone-branch.sh feature/x1

Technique 2: Workspace Management

bash
# Create a workspace script
#!/bin/bash
# setup-workspace.sh

mkdir -p ~/work/company-project
cd ~/work/company-project

# Clone main branches
git clone --depth 1 --single-branch --branch main https://github.com/company/project.git main
git clone --depth 1 --single-branch --branch development https://github.com/company/project.git development

# Create feature directories
mkdir features
cd features

echo "Workspace ready! Use:"
echo "cd ~/work/company-project/main     # Production work"
echo "cd ~/work/company-project/development # Development work"

Technique 3: Branch Switching with Minimal Data

bash
# If you need to switch branches in the same clone
git fetch --depth 1 origin main
git checkout main

# Work on main branch
vim src/main-file.js
git add src/main-file.js
git commit -m "Main branch changes"

# Switch back to development
git fetch --depth 1 origin development
git checkout development

# Merge main changes if needed
git merge main

Best Practices for Multi-Branch Work

1. Use Descriptive Directory Names

bash
# Good
project-development
project-main-hotfix
project-feature-auth

# Avoid
project
project2
project-new

2. Clean Up Regularly

bash
# Remove old branches when done
rm -rf project-feature-completed
rm -rf project-hotfix-done

# Keep only active work
ls ~/work/
# Output: project-development, project-main (current work only)

3. Use Branch-Specific Configurations

bash
# Different configs for different branches
# In project-development/.git/config
[user]
    name = Your Name
    email = [email protected]

# In project-main/.git/config  
[user]
    name = Your Name
    email = [email protected]

4. Document Your Workflow

Create a team wiki page:

markdown
# Multi-Branch Shallow Clone Workflow

## For New Features
1. `git clone --depth 1 --single-branch --branch development`
2. `git checkout -b feature/your-feature`
3. Work, commit, push
4. Create PR from web interface

## For Hotfixes
1. `git clone --depth 1 --single-branch --branch main`
2. `git checkout -b bug/your-fix`
3. Fix, commit, push
4. Create urgent PR

## For Reviews
1. `git clone --depth 1 --single-branch --branch feature/branch-name`
2. Review code
3. `rm -rf project-name` (cleanup)

Troubleshooting Multi-Branch Issues

Issue: "Branch not found"

bash
# Error: fatal: Remote branch main not found in upstream origin

Solution: Check available branches first:

bash
# List all remote branches
git ls-remote --heads origin

# Clone with correct branch name
git clone --depth 1 --single-branch --branch correct-branch-name https://github.com/company/project.git

Issue: "Cannot switch branches"

bash
# Error: fatal: 'main' did not match any file(s) known to git

Solution: Fetch the branch first:

bash
git fetch --depth 1 origin main
git checkout main

Issue: "Merge conflicts with shallow clone"

bash
# Error: fatal: refusing to merge unrelated histories

Solution: Use rebase or create new branch:

bash
# Option 1: Rebase
git rebase origin/development

# Option 2: Create new branch from target
git checkout -b feature/new-branch origin/development

The Takeaway

Working with multiple branches using shallow clones is not only possible but often more efficient than full clones. The key strategies are:

  1. Single branch focus - Work on one branch at a time
  2. Multiple directories - Separate clones for different branches
  3. Selective fetching - Get branches as needed
  4. Clean workspace - Remove old branches regularly
  5. Team coordination - Document workflows for consistency

Benefits:

  • 80%+ disk space savings compared to full clones
  • Faster cloning and switching
  • Better organization with separate directories
  • Easier cleanup when projects are complete

Remember: You don't need the full history to work effectively with multiple branches. Focus on what you need for your current work, and use shallow clones to keep your disk space under control.

Happy branching!


P.S. If you're dealing with disk space issues in your company, check out my post about why company projects fill up your disk and working with massive repositories for more strategies.