How to clone a specific branch in Git
Cloning a specific branch in Git allows you to download only the target branch without cloning the entire repository, saving time and bandwidth. As the creator of CoreUI, a widely used open-source UI library, I’ve guided thousands of contributors to clone specific feature branches for focused development work. From my expertise, the most effective approach is using the -b flag with git clone to target specific branches directly. This method reduces download time and disk usage while providing immediate access to the desired branch for development or testing.
Use git clone -b branch-name
to clone a specific branch directly from the repository.
# Clone specific branch
git clone -b develop https://github.com/username/repository.git
# Clone specific branch to custom directory
git clone -b feature/new-ui https://github.com/username/repository.git my-feature
# Clone specific branch with single history (shallow clone)
git clone -b main --single-branch --depth 1 https://github.com/username/repository.git
# Verify current branch after cloning
cd repository
git branch
git status
The -b
flag specifies which branch to checkout after cloning, while --single-branch
limits the clone to only that branch. Use --depth 1
for shallow clones that download only the latest commit to minimize bandwidth and storage requirements for large repositories.
Best Practice Note:
This is the same branch cloning approach we recommend for CoreUI contributors working on specific features.
Combine with --depth 1
for faster clones when you only need the latest code and don’t require the full commit history for your work.