bashbeginner

Git Clone Specific Branch

Clone a specific branch from a remote repository without fetching all branches and history.

bash
# Clone a specific branch only
git clone -b develop https://github.com/user/repo.git

# Clone a specific branch with only that branch's history
git clone -b develop --single-branch https://github.com/user/repo.git

# Clone into a specific directory
git clone -b develop https://github.com/user/repo.git ./my-project

# Clone a specific branch with limited depth
git clone -b develop --depth 1 https://github.com/user/repo.git

# Clone a tag
git clone -b v1.0.0 --depth 1 https://github.com/user/repo.git

# After single-branch clone, add other branches if needed
git remote set-branches --add origin main
git fetch origin main
git checkout main

# Verify which branch is checked out
git branch -a

Use Cases

  • Cloning only a deployment branch for CI/CD
  • Reducing clone time for large repositories
  • Checking out a specific release tag

Tags

Related Snippets

Similar patterns you can reuse in the same workflow.