To clone a specific branch in Git, use the following command:
bash
git clone -b <branch-name> --single-branch <repository-url>
Here:
- Replace
<branch-name>
with the name of the branch you want to clone. - Replace
<repository-url>
with the URL of the repository.
The -b
flag specifies the branch, and --single-branch
ensures only that branch is cloned, saving space and time.
Example:
bash
git clone -b feature-branch --single-branch https://github.com/user/repo.git
If you’ve already cloned the repository, you can switch to another branch using:
bash
git checkout <branch-name>