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


当前回答

使用我的工具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

其他回答

使用--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常见问题解答:如何克隆包含所有远程跟踪分支的存储库?

gitclone--原始repo上的镜像很好地实现了这一点。

git clone --mirror /path/to/original.git
git remote set-url origin /path/to/new-repo.git
git push -u origin

独立存储库

如果要查找包含所有远程分支和提交日志的自包含克隆或备份,请使用:

git clone http://user@repo.url
git pull --all

gitbranch-a的接受答案仅显示远程分支。如果您尝试签出分支,除非您仍然具有对源服务器的网络访问权限,否则将无法签出。

信贷:加布·科普利(Gabe Kopley)建议使用git pull-all。

注:当然,如果您不再具有远程/源服务器的网络访问权限,那么远程/源分支将不会在存储库克隆中反映任何更新。它们的修订将反映您执行上述两个存储库克隆命令的日期和时间的提交。


Checkout a *local* branch in the usual way with `git checkout remote/origin/` Use `git branch -a` to reveal the remote branches saved within your `clone` repository.

要使用一个命令将所有克隆分支签出到本地分支,请使用以下bash命令之一:

$ for i in $(git branch -a |grep 'remotes' | awk -F/ '{print $3}' \ 
| grep -v 'HEAD ->');do git checkout -b $i --track origin/$i; done

OR

如果您的回购具有嵌套分支,则此命令还将考虑这一点:

for i in $(git branch -a |grep 'remotes' |grep -v 'HEAD ->');do \
basename ${i##\./} | xargs -I {} git checkout -b {} --track origin/{}; done

上述命令将将本地分支签入本地git存储库,其名称与remote/orging/<branchname>相同,并将其设置为--如果您再次重新获得对原始存储库服务器的网络访问并以通常方式执行git pull命令,则跟踪远程/orging服务器上远程分支的更改。

从本地回购中克隆不能与gitclone和gitfetch一起使用:许多分支/标签将保持不匹配。

获取包含所有分支和标记的克隆。

git clone --mirror git://example.com/myproject myproject-local-bare-repo.git

要获取包含所有分支和标记但也包含工作副本的克隆,请执行以下操作:

git clone --mirror git://example.com/myproject myproject/.git
cd myproject
git config --unset core.bare
git config receive.denyCurrentBranch updateInstead
git checkout master

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

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