Git Cloning Repositories Clone a specific branch


To clone a specific branch of a repository, type --branch <branch name> before the repository url:

git clone --branch <branch name> <url> [directory]

To use the shorthand option for --branch, type -b. This command downloads entire repository and checks out <branch name>.

To save disk space you can clone history leading only to single branch with:

git clone --branch <branch_name> --single-branch <url> [directory]

If --single-branch is not added to the command, history of all branches will be cloned into [directory]. This can be issue with big repositories.

To later undo --single-branch flag and fetch the rest of repository use command:

git config remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*"
git fetch origin