我的主分支和开发分支在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(参见本文)。

其他回答

使用我的工具git_remote_branch(grb)。您需要在机器上安装Ruby)。它是专门为使远程分支操作非常容易而构建的。

每次它代表您执行操作时,都会在控制台上以红色打印。随着时间的推移,它们最终会进入你的大脑:-)

如果您不希望grb代表您运行命令,请使用“解释”功能。这些命令将打印到您的控制台,而不是为您执行。

最后,所有命令都有别名,以便于记忆。

注意,这是alpha软件;-)

以下是运行grb帮助时的帮助:

git_remote_branch version 0.2.6

  Usage:

  grb create branch_name [origin_server]

  grb publish branch_name [origin_server]

  grb rename branch_name [origin_server]

  grb delete branch_name [origin_server]

  grb track branch_name [origin_server]



  Notes:
  - If origin_server is not specified, the name 'origin' is assumed
    (git's default)
  - The rename functionality renames the current branch

  The explain meta-command: you can also prepend any command with the
keyword 'explain'. Instead of executing the command, git_remote_branch
will simply output the list of commands you need to run to accomplish
that goal.

  Example:
    grb explain create
    grb explain create my_branch github

  All commands also have aliases:
  create: create, new
  delete: delete, destroy, kill, remove, rm
  publish: publish, remotize
  rename: rename, rn, mv, move
  track: track, follow, grab, fetch

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克隆应该复制整个存储库。尝试克隆它,然后运行gitbranch-a。它应该列出所有分支。如果您想切换到分支“foo”而不是“master”,请使用git checkout foo。

执行远程存储库的裸克隆,将内容保存到.git目录

git clone --bare remote-repo-url.git localdirname/.git

(使用gitclone--bare或gitinit--bare创建的裸git存储库是一个存储存储库,它没有工作目录,您无法在其中创建或修改文件。)

将目录更改为本地目录

cd localdirname

使您的git存储库可修改

git config --bool core.bare false

还原工作目录

git reset --hard

列出所有分支机构

git branch -al

只需执行以下操作:

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

$ cd myproject

$ git checkout branchxyz
Branch branchxyz set up to track remote branch branchxyz from origin.
Switched to a new branch 'branchxyz'

$ git pull
Already up-to-date.

$ git branch
* branchxyz
  master

$ git branch -a
* branchxyz
  master
  remotes/origin/HEAD -> origin/master
  remotes/origin/branchxyz
  remotes/origin/branch123

你看,gitclonegit://example.com/myprojectt获取所有内容,甚至是分支,您只需签出它们,就可以创建本地分支。