【问题标题】:Git: Clone from a branch other than masterGit:从 master 以外的分支克隆
【发布时间】:2013-06-27 07:40:15
【问题描述】:

我正在尝试从 Github 的存储库中提取。但我不想克隆主分支。我想克隆其他一些分支。当我尝试git clone <url> 时,我从主分支获取文件。我该怎么办?

另外,假设代码库中的代码已更新,我想获取最新的代码,是否应该再次使用 git clone ?因为项目规模巨大。另外,如果我在本地对项目进行更改,然后再次使用 git clone,我所做的更改还会存在吗?如果我不想进行更改怎么办?

我什至不确定git clone 是否是正确的命令。 git pullgit fetch?

对不起,我对 git 很陌生。

【问题讨论】:

标签: git github github-for-windows


【解决方案1】:

试试这个:

git init
git fetch url-to-repo branchname:refs/remotes/origin/branchname

编辑

更好的解决方案:

git clone -b mybranch --single-branch git://sub.domain.com/repo.git

【讨论】:

  • 我在 url-to-repo 之后没有得到该部分。我应该只提到分支名称吗?例如- git fetch demo(假设 dev 是分支名称?)
  • 我换了一个更清洁的解决方案
  • 顺便说一句,第一个解决方案:它说“branchname”应该说你想要克隆的分支git fetch my_repo_url demo:refs/remotes/origin/demo
  • 好的,谢谢。我试过git branch -l 并得到了所有的分支。我正在尝试第二种解决方案。
  • '--single-branch' 有什么作用?
【解决方案2】:
git clone <url>

为每个分支克隆并创建远程跟踪分支。 如果您想查看可用的分支(克隆后),请键入

git branch -l

克隆后切换到特定分支:

git checkout <branchname>

其中 branchname 是分支的名称 :)

如果你想克隆并签出一个特定的分支

git clone -b <branchname> <url>

您提到的其他命令用于“更新”您当前的工作副本。 git pull 从远程存储库中获取所有更改并将它们合并,而 git fetch 仅获取它们而不合并。

【讨论】:

  • 其实我觉得是git clone -b &lt;branchname&gt; &lt;url&gt;
  • 感谢您的回答 :) 但我只能选择一个答案作为正确答案。我也不能赞成您的回答,因为它至少需要 15 个声望点。
  • 幸运的是,git clone &lt;url&gt; -b &lt;branchname&gt; 的订单也可以。这对需求文件很有用。您可能在构建脚本中有while read; do git clone $REPLY; done &lt; requirements.txt。该文件中的大多数条目只是 URL,但您也可以将 -b &lt;branchname&gt; 添加到任何条目的末尾。
  • git clone -b ,这解决了我的问题。
  • 我这辈子都没见过也没用过git branch -l!我检查了手册页。事实证明,只要您拨打git branch,这就是默认设置,这是我一生都在做的事情。 -l 真的让我失望了。来自man git branchIf --list is given, or if there are no non-option arguments, existing branches are listed
【解决方案3】:

使用git clone --branch &lt;name&gt; 可能添加--single-branch

像往常一样,您可以通过git clone --help 阅读有关命令的详细信息

【讨论】:

    【解决方案4】:

    如果您已经克隆了 repo 并希望为 master 以外的分支做出贡献,请执行以下操作:

    $ git checkout --track origin/<branch-name>
    

    当然,一定要指定正确的遥控器名称(上例中的origin)

    此命令将创建一个新的本地分支,该分支将跟踪特定的远程分支。

    【讨论】:

      【解决方案5】:

      这里已经有一些答案了:

      How do I clone a single branch in Git?

      git clone --branch --single-branch []

      git clone --single-branch

      加号

      【讨论】:

        猜你喜欢
        • 2017-06-17
        • 2021-06-26
        • 1970-01-01
        • 1970-01-01
        • 2014-07-19
        • 1970-01-01
        • 2012-02-11
        • 2018-09-08
        • 2016-05-02
        相关资源
        最近更新 更多