git clone
command is used to clone branches and git repositories. In this tutorial, we will learn how to clone a git branch in different ways. We will learn clone single branch, clone to a specific folder, clone specific tag etc in this tutorial.
List Branches
We will start by listing branches. In order to copy branch, we need to list the branches. We will just use branch
command which will list currently existing local and remote branches.
$ git branch

Clone Single Specified Branch
We can clone a single specific branch by using --single-branch
with the --branch
option. We will also provide the branch name. This will save from the disk space where only specified branch and related data will be copied.
$ git clone --single-branch --branch master https://github.com/nmap/nmap.git

Clone To Specific Folder
We can copy a specific branch to the specified folder. We will just add the folder name to the end of the command. This folder will be created automatically. In this example, we will copy the master branch of the nmap into a folder named test
.
$ git clone --single-branch --branch master https://github.com/nmap/nmap.git test

Clone Specific Tag
We have the ability to clone specific tag. Actually, tag is the name of the branch but tag will provide a different point of view. We will use -branch
option and provide the tag we want to copy. We will copy tag named master
in this example.
$ git clone --single-branch --branch master https://github.com/nmap/nmap.git
Shallow Clone or Specify Depth
Branch copy operation will all previous versions of the specified branch or tag. In some cases, this may create problems like disk space or network bandwidth. In this example, we will use --depth
option and provide the number of depth which is 2 in this case.
$ git clone --depth=2 --single-branch --branch master https://github.com/nmap/nmap.git
