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


当前回答

截至2017年初,该评论中的答案有效:

gitfetch<origin name><branch name>为您关闭分支。虽然这不会同时拉动所有分支,但您可以对每个分支单独执行此操作。

其他回答

当您执行“gitclone”时git://location“,将获取所有分支和标记。

为了在特定远程分支上工作,假设它是源远程分支:

git checkout -b branch origin/branchname

我试图找出如何删除我在本地删除的远程分支。起源不是我的,我不想经历重新克隆一切的麻烦。

这对我有用:

假设您需要在本地重新创建分支:

git checkout -b recreated-branch-name
git branch -a (to list remote branches)
git rebase remotes/remote-origin/recreated-branch-name

因此,如果我从gituser/master分支到sjp,然后将其分支到sjp/mynewbranch,则如下所示:

$ git checkout -b mynewbranch

$ git branch -a
  master
  remotes/sjp/master
  remotes/sjp/mynewbranch

$ git fetch (habit to always do before)

$ git rebase remotes/sjp/mynewbranch

使用--mirror选项似乎可以正确复制远程跟踪分支。然而,它将存储库设置为一个裸存储库,因此您必须在之后将其恢复为一个正常的存储库。

git clone --mirror path/to/original path/to/dest/.git
cd path/to/dest
git config --bool core.bare false
git checkout anybranch

参考:Git常见问题解答:如何克隆包含所有远程跟踪分支的存储库?

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(参见本文)。

首先,克隆一个远程Git存储库并将其cd到其中:

$ git clone git://example.com/myproject
$ cd myproject

接下来,查看存储库中的本地分支:

$ git branch
* master

但您的存储库中隐藏着其他分支!使用-a标志查看这些:

$ git branch -a
* master
  remotes/origin/HEAD
  remotes/origin/master
  remotes/origin/v1.0-stable
  remotes/origin/experimental

要快速查看上游分支,请直接查看:

$ git checkout origin/experimental

要处理该分支,请创建一个本地跟踪分支,该分支通过以下方式自动完成:

$ git checkout experimental

Branch experimental set up to track remote branch experimental from origin.
Switched to a new branch 'experimental'

这里,“新分支”只是指从索引中获取分支并在本地为您创建。正如前一行告诉的那样,正在设置分支以跟踪远程分支,这通常意味着origin/branch_name分支。

您的本地分支机构现在应该显示:

$ git branch
* experimental
  master

您可以使用gitremote跟踪多个远程存储库:

$ git remote add win32 git://example.com/users/joe/myproject-win32-port
$ git branch -a
* master
  remotes/origin/HEAD
  remotes/origin/master
  remotes/origin/v1.0-stable
  remotes/origin/experimental
  remotes/win32/master
  remotes/win32/new-widgets

在这一点上,事情变得非常疯狂,所以运行gitk看看发生了什么:

$ gitk --all &