How to clone a specific branch in git?

Posted: | Last updated: | less than 1 minute read

Sometime engineers need to clone specific remote branch. This blog will help you to clone specific branch from remote.

Syntax

You need to specify the branch name with -b command switch. Here is the syntax of the command to clone the specific git branch.

$ git clone -b <BRANCH_NAME>  <GIT_REMOTE_URL>

Examples

Below are 2 methods to clone single and specific git branch!

Single branch with others details

The following command will clone the branch v1.0_travis_demo from the vermanotes/docker-spring-boot git repository.

$ git clone -b v1.0_travis_demo https://github.com/vermanotes/docker-spring-boot.git

The above command clones only the specific branch but fetches the details of other branches. You can view all branches details with command git branch -a

Only single branch

You can prevent fetching details for other branches by using --single-branch option as given below.

$ git clone -b v1.0_travis_demo --single-branch https://github.com/vermanotes/docker-spring-boot.git

You can view the active branch by running the command:

$ git branch

Happy coding!