When working with Git repositories, sometimes you only need a specific branch instead of cloning the entire project with all branches. Git makes it easy to clone a particular branch directly, which saves time and disk space—especially with large repositories.
In this guide, we’ll show you how to clone a specific branch using Git.
✅ Prerequisites
Before you begin:
- Ensure Git is installed on your machine.
- Have access to the repository URL.
- Know the branch name you want to clone.
🔹 Option 1: Clone a Specific Branch (Standard Way)
You can clone a specific branch and set it as the current working branch:
git clone --branch <branch-name> <repo-url>
📌 Example:
git clone --branch develop https://github.com/username/project.git
This clones the
develop
branch and checks it out directly.
⚠️ Note: This still downloads the entire repository history.
🔹 Option 2: Clone Only a Single Branch (With No Other Branches)
To clone only the history of a single branch, use the --single-branch
flag:
git clone --branch <branch-name> --single-branch <repo-url>
📌 Example:
git clone --branch feature-xyz --single-branch https://github.com/username/project.git
This ensures:
- Only the
feature-xyz
branch is fetched. - Other branches are ignored (saving bandwidth and space).
🔹 Bonus: Switch Branches After Cloning
If you’ve already cloned the repository and want to switch to another branch:
git fetch origin
git checkout <branch-name>
Example:
git checkout hotfix/login-issue
✅ Summary
Task | Command Example |
---|---|
Clone a specific branch | git clone --branch dev https://github.com/user/repo.git |
Clone only one branch (lightweight) | git clone --branch dev --single-branch <repo-url> |
Switch to another branch after cloning | git checkout branch-name |
🚀 Final Thoughts
Cloning a particular branch in Git is a useful skill when working with feature branches, large repositories, or when you only need a subset of the project. With just a couple of flags, you can streamline your workflow and improve efficiency.