我在~/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?


当前回答

有点晚了,但我想添加我用来解决这个问题的解决方案。我在这里找到了答案。

无论如何,问题似乎是在问“如何从另一个回购的分支启动一个新项目?”

对此,我使用的解决方案是首先在github或任何地方创建一个新的回购。这将作为新项目的回购。

在本地机器上,导航到具有要用作新项目模板的分支的项目。

执行如下命令:

git push https://github.com/accountname/new-repo.git +old_branch:master

这将做的是将old_branch推到new-repo,并使其成为new repo的主分支。

然后,您只需将新的repo复制到新项目的本地目录中,就可以在旧分支中启动一个新项目。

其他回答

如果你想要一个浅克隆,你可以这样做:

git clone -b mybranch --depth=1 https://example.com/myproject.git localname

——depth=1表示——single-branch。

让我们以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分支。

类似于@nosaiba-darwish在这里说的:这里

我们公司通常是这样做的:

git clone -b <name_of_branch> --single-branch <git_url> folder_to_clone_locally

打开cmd。

cd folder_name  # enter the path where to clone the branch

只有一个命令:

git clone url_of_projecturltoclone -b branch_name
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}