How do I clone a specific Git branch from a remote repository?
I understand that Git clone typically downloads all branches, but I only need a specific branch. Is there a way to clone just one branch without having to switch branches on the remote repository? I’m looking for a command or method that allows me to specify which branch to clone during the initial clone operation.
To clone a specific Git branch from a remote repository, you can use the git clone --branch <branchname> --single-branch <url> command, which downloads only the specified branch and its history rather than cloning all branches. This approach saves disk space and cloning time by avoiding the download of unnecessary branches and their commit histories. The --single-branch flag ensures that only the target branch is cloned, while the --branch (or -b shorthand) option specifies which branch to checkout.
Contents
- Basic Commands for Cloning Specific Branches
- Understanding --single-branch vs Regular Clone
- Shallow Clones for Specific Branches
- Practical Examples and Use Cases
- Common Issues and Troubleshooting
- Best Practices
Basic Commands for Cloning Specific Branches
The primary method for cloning a specific branch involves using the --branch option combined with --single-branch. Here are the essential commands:
Standard Single Branch Clone
git clone --branch <branch-name> --single-branch <repository-url>
This command clones only the specified branch from the remote repository. For example:
git clone --branch feature-auth --single-branch https://github.com/user/repo.git
Using Short-form Options
The -b flag serves as a shorthand alias for --branch, making the command more concise:
git clone -b <branch-name> --single-branch <repository-url>
Without --single-branch
If you omit --single-branch, Git will clone all remote branches but only checkout the specified one:
git clone --branch <branch-name> <repository-url>
According to the Atlassian Git Tutorial, “The -branch argument lets you specify a specific branch to clone instead of the branch the remote HEAD is pointing to, usually the main branch. In addition you can pass a tag instead of branch for the same effect.”
Understanding --single-branch vs Regular Clone
Key Differences
| Feature | With --single-branch | Without --single-branch |
|---|---|---|
| Branches Downloaded | Only specified branch | All remote branches |
| Disk Usage | Minimal | Potentially large |
| Clone Time | Faster | Slower |
| Local Branches | Only one branch | All remote branches available |
When to Use Each Approach
Use --single-branch when:
- You only need one specific branch
- You’re working with large repositories
- You want to minimize download time and disk usage
- You’re setting up CI/CD pipelines with specific requirements
Without --single-branch when:
- You need access to multiple branches
- You’re working on a team and might need to switch between branches
- You’re unsure which branch you’ll need most frequently
As Stack Overflow explains: “by invoking git clone --branch <branchname> url you’re fetching all the branches and checking out one. That may, for instance, mean that your repository has a 5kB documentation or wiki branch and 5GB data branch.”
Shallow Clones for Specific Branches
Shallow clones (--depth) are particularly useful when combined with single-branch cloning to create minimal clones:
Basic Shallow Clone
git clone --depth 1 --branch <branch-name> --single-branch <repository-url>
Why Shallow + Single Branch is Powerful
Shallow clones limit the history to a specified number of commits (default is 1), which when combined with --single-branch creates an extremely minimal clone:
- Reduced Transfer Size: Only downloads the latest commit(s) of the specified branch
- Faster Cloning: Minimal data transfer
- Limited History: Only recent commit history available
As The GitHub Blog notes: “Shallow clones are best combined with the --single-branch --branch=<branch> options as well, to ensure we only download the data for the commit we plan to use immediately.”
Shallow Clone Implications
Important: When you use
--depthwith--single-branch, Git automatically applies--single-branchunless you explicitly specify--no-single-branch. This means shallow clones are inherently single-branch operations unless you override this behavior.
The Git Documentation states: “Implies --single-branch unless --no-single-branch is given to fetch the histories near the tips of all branches.”
Practical Examples and Use Cases
Example 1: Cloning a Feature Branch
# Clone only the feature-login branch
git clone --branch feature-login --single-branch https://github.com/myorg/myapp.git myapp-login
Example 2: Shallow Clone for CI/CD
# Minimal clone for continuous integration
git clone --depth 1 --branch main --single-branch https://github.com/myorg/myapp.git
Example 3: Cloning with Different Directory Name
# Clone to a specific directory name
git clone -b develop --single-branch https://github.com/myorg/myapp.git myapp-develop
Example 4: Cloning Tags Instead of Branches
You can also clone tags using the same syntax:
git clone --branch v1.2.3 --single-branch https://github.com/myorg/myapp.git
Example 5: Post-Clone Branch Management
After cloning a single branch, you can add other branches later if needed:
# Add the remote tracking for another branch
git remote set-branches origin main production
git fetch --depth 1 origin main production
Common Issues and Troubleshooting
Issue: “pathspec ‘branch-name’ did not match any file(s) known to git”
This occurs when trying to checkout a branch that wasn’t cloned. With --single-branch, only the specified branch is available locally.
Solution: Either clone without --single-branch or fetch additional branches:
# Fetch additional branches after single-branch clone
git remote set-branches origin main develop
git fetch origin main develop
Issue: Missing Remote Branches
After cloning with --single-branch, you won’t see other branches when running git branch -a.
Solution: Use git remote update or fetch specific branches:
git remote update --prune
Issue: Shallow Clone Limitations
Shallow clones have limited history, which can cause issues with certain Git operations.
Solution: Use --depth with a higher number or avoid shallow clones for complex workflows:
# Clone with more history
git clone --depth 10 --branch main --single-branch <repository-url>
Best Practices
When to Use Single-Branch Cloning
- CI/CD Pipelines: Use
--single-branchwith--depth 1for faster builds - Feature Development: Clone only the branch you’re working on
- Large Repositories: Save bandwidth and disk space
- Team Collaboration: Clone only relevant branches for your current task
Command Recommendations
For most development scenarios:
git clone -b <branch-name> --single-branch <repository-url>
For CI/CD and deployment:
git clone --depth 1 -b <branch-name> --single-branch <repository-url>
For exploratory work where you might need multiple branches:
git clone --branch <branch-name> <repository-url>
Performance Considerations
- Large repositories: Always use
--single-branchfor significant space savings - Frequent switching: Consider cloning multiple branches if you switch often
- Network constraints: Use shallow clones for slow connections
- Storage optimization: Combine
--single-branchwith--depthfor minimal footprint
Conclusion
Cloning a specific Git branch is straightforward using the --branch and --single-branch options. The key takeaways are:
- Use
git clone --branch <branch-name> --single-branch <url>to clone only the specified branch - The
-bflag provides a convenient shorthand for--branch - Shallow clones (
--depth) combined with single-branch cloning create minimal, fast clones - Without
--single-branch, Git downloads all branches but only checks out the specified one - Single-branch clones save significant disk space and reduce cloning time, especially for large repositories
Choose your approach based on your specific needs - single-branch cloning for focused work, regular cloning for flexible development, or shallow clones for CI/CD scenarios where minimal footprint is essential.
Sources
- How to Clone a Branch in Git? | Atlassian Git Tutorial
- How do I clone a specific Git branch? - Stack Overflow
- Git - git-clone Documentation
- Get up to speed with partial clone and shallow clone - The GitHub Blog
- How to Clone a Specific Git Branch | Delft Stack
- How to Clone single branch from git repository - YouTube
- Git Clone Specific Branch: A How-To Guide | Career Karma