Appearance
How to Clone a Single Branch in Git
Sometimes it is necessary to clone a single and specific branch. Let’s take a look at different approaches and take the steps to get the job done!
Cloning a Single Branch Using git remote add
Creating a new repository
The first step should be creating a new repository with git init:
git init
bash
git initAdding the remote and fetching the branch
The second step is to add a remote named origin for the given repository, which will fetch the given branch from origin:
git remote add
bash
git remote add -t <branch-name> -f origin <remote-repo-url>Checkout to the given branch
The third step is to switch to that branch with the git switch command for creating a corresponding local branch:
How to Clone a Single Branch in Git
bash
git switch <branch-name>Cloning a Single Branch Using git clone
The classic git clone command with the --single-branch option will clone only the master branch by default. If you want to clone another branch, you should add the --branch flag with the name of the desired branch.
git clone a single branch
bash
git clone --single-branch --branch <branch-name> --depth 1 <remote-repo-url>