我的主分支和开发分支在GitHub上被远程跟踪。如何克隆这两个分支?


当前回答

Git通常(如果未指定)从一个或多个其他存储库中获取所有分支和/或标记(refs,请参见:Git-ls-refs)以及完成其历史所需的对象。换句话说,它获取已经下载的对象可以访问的对象。看:git fetch真正做什么?

有时,您可能有一些分支/标记没有直接连接到当前的分支/标记,因此git pull-all/git fetch-all在这种情况下不会有帮助,但您可以通过以下方式列出它们:

git ls-remote -h -t origin

并通过知道引用名称手动获取它们。

因此,要获取所有信息,请尝试:

git fetch origin --depth=10000 $(git ls-remote -h -t origin)

如果您对存储库进行了浅处理,--depth=10000参数可能会有所帮助。

然后再次检查所有分支:

git branch -avv

如果上述方法无效,则需要手动将丢失的分支添加到跟踪列表中(因为它们不知何故丢失了):

$ git remote -v show origin

...
  Remote branches:
    master      tracked

通过git远程设置分支,如:

git remote set-branches --add origin missing_branch

因此在获取后,它可能出现在remotes/origin下:

$ git remote -v show origin

...
  Remote branches:
    missing_branch new (next fetch will store in remotes/origin)
$ git fetch
From github.com:Foo/Bar
 * [new branch]      missing_branch -> origin/missing_branch

故障排除

如果您仍然无法获得主分支以外的任何内容,请检查以下内容:

仔细检查遥控器(gitremote-v),例如。验证git-config-branch.master.remote是源代码。通过:git remote show origin检查origin是否指向正确的URL(参见本文)。

其他回答

使用您可以记住的命令

我正在使用Bitbucket,一个Atlassian的存储库托管服务。所以我试着遵循他们的文档。这对我来说非常有用。使用以下简单而简短的命令,您可以签出远程分支。

首先克隆存储库,然后转到目标文件夹。最后,但并非最不重要的是,取件和结账:

git clone <repo> <destination_folder>
cd <destination_folder>
git fetch && git checkout <branch>

就是这样。这里有一个更真实的例子:

git clone https://username@bitbucket.org/team/repository.git project_folder
cd project_folder
git fetch && git checkout develop

您将在文档中找到有关命令的详细信息:克隆命令、获取命令、签出命令

对于使用Visual Studio代码的开发人员来说,一个更好的替代解决方案是使用Git Shadow Extension。

此Visual Studio代码扩展允许克隆存储库内容和目录,这些内容和目录可以通过分支名称或提交哈希进行筛选。这样,分支或提交可以用作新项目的样板/模板。

我认为这样做很有效:

mkdir YourRepo
cd YourRepo
git init --bare .git                       # create a bare repo
git remote add origin REMOTE_URL           # add a remote
git fetch origin refs/heads/*:refs/heads/* # fetch heads
git fetch origin refs/tags/*:refs/tags/*   # fetch tags
git init                                   # reinit work tree
git checkout master                        # checkout a branch

到目前为止,这对我有效。

我正在用Python和Pytest从Udemy课程Elegant Automation Frameworks中克隆一个存储库,以便以后可以离线查看。我尝试下载zip,但这只适用于当前分支,因此这是我的2美分。

我在Windows上工作,显然,我求助于Windows Subsystem for Linux中的Ubuntu shell。克隆完成后,我的分支如下:

$ git clone https://github.com/BrandonBlair/elegantframeworks.git

$ git branch -a

* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/config_recipe
  remotes/origin/functionaltests
  remotes/origin/master
  remotes/origin/parallel
  remotes/origin/parametrize
  remotes/origin/parametrize_data_excel
  remotes/origin/unittesting
  remotes/origin/unittesting1

然后,在撞了几块几位数的结账砖墙之后,我终于明白了:

$ for b in `git branch -a | cut -c18- | cut -d\  -f1`; do git checkout $b; git stash; done

在此之后,以下是我的分支:

$ git branch -a

  config_recipe
  functionaltests
  master
  parallel
  parametrize
  parametrize_data_excel
  unittesting
* unittesting1
  remotes/origin/HEAD -> origin/master
  remotes/origin/config_recipe
  remotes/origin/functionaltests
  remotes/origin/master
  remotes/origin/parallel
  remotes/origin/parametrize
  remotes/origin/parametrize_data_excel
  remotes/origin/unittesting
  remotes/origin/unittesting1

我的是物理的,去掉了最初的遥控器/原点,然后过滤空间分隔符。可以说,我本可以把HEAD去掉,只剪一刀,但我会留给评论。

请注意,您当前的分支机构现在是列表中的最后一个。如果你不知道这是为什么,那你的处境就很艰难。现在你想要什么就结账吧。

关于

git checkout-b实验来源/实验

使用

git checkout -t origin/experimental

或者更冗长,但更容易记住

git checkout --track origin/experimental

在跟踪远程存储库方面可能更好。