我在~/local_repo有一个本地Git存储库。它有几个分支:

$ git branch
* master
  rails
  c
  c++

要克隆本地存储库,我做:

$ git clone ~/local_repo new_repo
Initialized empty Git repository in /home/username/new_repo/.git/

new_repo master分支指向local_repo master分支,我可以推/拉。

但我无法克隆另一个分支。我只想拉我想要的分支(例如rails),这样新的存储库就有一个主分支,默认情况下,它可以推入和拉出local_repo的rails分支。我如何实现这一点,或者也许类似于local_repo跟踪主local_repo?


当前回答

从git-clone手册页:

-单分支是你克隆的朋友 记住使用——branch <分支名>或者只克隆远程主HEAD(默认为master)

总是记得按Ctrl + F5来读取新源代码,而不是从缓存中读取:-) (我很长一段时间都不知道这个选项。)

其他回答

打开cmd。

cd folder_name  # enter the path where to clone the branch

只有一个命令:

git clone url_of_projecturltoclone -b branch_name

要克隆一个你没有公钥的Git分支,使用这个:

git clone -b <branch> <Git repository URL or clone URL you get from Git repository>

你可以试试长篇大论的方式:

mkdir newrepo.git
cd newrepo.git
git init
git remote add origin file:///path/to/original
git fetch origin branchiwant:refs/remotes/origin/branchiwant
git checkout -b branchiwant --track origin/branchiwant

它的作用是:

创建并初始化一个空Git存储库。 将原始存储库添加为名为origin的远程存储库。 只从称为origin的远端获取所需的分支。 创建并签出一个新分支,该分支被设置为跟踪刚刚克隆的源分支。

希望这是你所追求的东西。

git clone --branch {branch-name} {repo-URI}

例子:

git clone --branch dev https://github.com/ann/cleaningmachine.git

dev:这是{branch-name} https://github.com/ann/cleaningmachine.git:这是{repo-URI}

让我们以flask repo为例。除master外,它还有3个分支。让我们签出1.1。X远程分支

克隆git回购

git clone https://github.com/pallets/flask

CD进入回购。

cd flask

获取远程分支1.1.x

git fetch origin 1.1.x

签出分支

git checkout 1.1.x

您将切换到分支1.1。X,它将跟踪远程1.1。x分支。