克隆特定的 Git 分支

Kevin Amayi 2023年1月30日
  1. 通过获取所有分支并签出到特定分支,从远程仓库克隆特定 Git 分支
  2. 直接从远程仓库克隆特定的 Git 分支
克隆特定的 Git 分支

本文将讨论通过获取所有分支并签出特定分支来从远程仓库克隆特定 git 分支。

通过获取所有分支并签出到特定分支,从远程仓库克隆特定 Git 分支

我们将克隆一个包含两个分支的远程仓库,即 mastergh-pages,然后切换到 gh-pages 分支。

<!-- The command to use is -->
git clone <remote-repo-url>

<!-- From your terminal run -->
git clone https://github.com/KEVINAMAYI/AkanNameGenerator.git

输出:

Cloning into 'AkanNameGenerator'...
remote: Enumerating objects: 94, done.
remote: Total 94 (delta 0), reused 0 (delta 0), pack-reused 94
Unpacking objects: 100% (94/94), 2.38 MiB | 1.86 MiB/s, done.

我们将进入项目文件夹并使用以下命令列出可用的分支。

<!-- get into project folder -->
cd AkanNameGenerator

<!-- List branches available -->
git branch -a

输出:

<!-- The asterix indicates we are on branch main -->
* main
remotes/origin/HEAD -> origin/main
remotes/origin/gh-pages
remotes/origin/main

我们将使用下面的命令切换到特定的分支 gh-pages

git checkout gh-pages

我们将通过运行确认我们在特定的分支 gh-pages 中:

git branch

输出:

<!-- The asterix indicates we are now on branch gh-pages -->
* gh-pages
main

直接从远程仓库克隆特定的 Git 分支

我们将通过在 Git 命令中指定分支名称,直接从远程仓库克隆我们需要的特定分支。

<!-- The command to use is -->
git clone --branch <branchname> --single-branch <remote-repo-url>

<!-- From your terminal run -->
git clone --branch gh-pages --single-branch https://github.com/KEVINAMAYI/AkanNameGenerator.git

输出:

Cloning into 'AkanNameGenerator'...
remote: Enumerating objects: 94, done.
remote: Total 94 (delta 0), reused 0 (delta 0), pack-reused 94
Unpacking objects: 100% (94/94), 2.38 MiB | 231.00 KiB/s, done.

我们将进入项目文件夹并通过运行以下命令列出可用的分支。

<!-- get into project folder -->
cd AkanNameGenerator

<!-- List branches available -->
git branch -a

输出:

<!-- we have only our specific branch -->
* gh-pages

相关文章 - Git Branch