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.
git clone
命令用于克隆分支和git存储库。 在本教程中,我们将学习如何以不同方式克隆git分支。 在本教程中,我们将学习克隆单个分支,克隆到特定文件夹,克隆特定标签等。
列出分支 (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.
我们将从列出分支开始。 为了复制分支,我们需要列出分支。 我们将只使用branch
命令,它将列出当前存在的本地和远程分支。
$ 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.
我们可以使用--single-branch
和--branch
选项来克隆单个特定分支。 我们还将提供分支名称。 这将节省仅复制指定分支和相关数据的磁盘空间。
$ 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
.
我们可以将特定的分支复制到指定的文件夹。 我们将文件夹名称添加到命令末尾。 该文件夹将自动创建。 在此示例中,我们将nmap的master分支复制到名为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.
我们有能力克隆特定标签。 实际上,标签是分支的名称,但是标签将提供不同的观点。 我们将使用-branch
选项并提供要复制的标签。 在此示例中,我们将复制名为master
标签。
$ 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.
分支复制操作将指定分支或标记的所有先前版本。 在某些情况下,这可能会产生磁盘空间或网络带宽之类的问题。 在此示例中,我们将使用--depth
选项,并在这种情况下提供2的深度数。
$ git clone --depth=2 --single-branch --branch master https://github.com/nmap/nmap.git
翻译自: https://www.poftut.com/how-to-clone-git-branch-command-tutorial-with-examples/