Git Clone Specific Branch

Git is different from other centralized version control systems where it provides the ability to work in a distributed manner. Every developer has the option to control the full history of their code locally and remotely. As a flexible centralized version control system, how can we clone a specific branch in Git?

Git Clone Specific Branch Syntax

Git provides the clone command in order to clone local and remote branches. In order to clone a specific branch the git clone command is used where its syntax is like below.

git clone --branch BRANCH_NAME REMOTE_REPOSITORY
  • BRANCH_NAME is the branch which will be cloned.
  • REMOTE_REPOSITORY is the remote repository where the branch will be cloned from.

Clone Specific Branch with “git clone”

The “git clone” command is used to clone a single branch from a remote repository. In the following example, we clone the branch named testing from remote repository [email protected]:app/my.git . The --branch option is used to specify the branch name.

$ git clone --branch testing [email protected]:app/my.git

Alternatively, the -b option can be used to specify branch name instead of –branch option.

$ git clone --branch testing [email protected]:app/my.git

Clone Specific Branch with “git clone –single-branch”

With Git 1.7.10 a new option is added. The --single-branch option is used to only clone specified branches. This option prevents fetching of all branches.

$ git clone --branch testing --single-branch [email protected]:app/my.git

Leave a Comment