When working with Git, you may not always need the entire repository with all branches — sometimes, you just want a specific branch. Cloning a particular branch can save time and disk space, especially in large projects.
This guide shows you how to clone a specific branch using Git.
✅ Method 1: Clone Specific Branch and Its History
To clone a specific branch from a remote repository:
git clone --branch branch-name https://github.com/username/repo-name.git
Or shorthand:
git clone -b branch-name https://github.com/username/repo-name.git
🔍 Example:
git clone -b feature/login https://github.com/myuser/project.git
This clones the entire repository but checks out the specified branch.
✅ Method 2: Clone Specific Branch Without Full History (Shallow Clone)
For a lightweight clone (faster, less storage), use:
git clone --branch branch-name --depth 1 https://github.com/username/repo-name.git
This clones only the latest snapshot of the branch without full history.
✅ Method 3: Clone Entire Repo, Then Checkout a Branch
If you’ve already cloned the repo or want access to all branches:
git clone https://github.com/username/repo-name.git
cd repo-name
git checkout branch-name
🧠 Quick Comparison
Method | Command |
---|---|
Clone specific branch (full history) | git clone -b branch-name <url> |
Shallow clone (latest commit only) | git clone -b branch-name --depth 1 <url> |
Clone then switch | git checkout branch-name after clone |
🏁 Conclusion
Cloning a specific branch in Git is efficient and flexible. Whether you’re looking to work on a feature, fix a bug, or save bandwidth, knowing how to clone just the branch you need is a handy skill.