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


当前回答

好吧,当你克隆你的回购时,你有所有的分支。。。

如果你只是做git分支,它们是隐藏的。。。

所以,如果您想查看所有分支的名称,只需添加--all标志,如下所示:

gitbranch—all或gitbranch-a

如果你只是到分行结账,你就能得到你所需要的一切。

但是,如果你克隆后,其他人创建了分支,该怎么办?

在这种情况下,只需执行以下操作:

git获取

并再次检查所有分支。。。

如果您想同时取件和结账,可以执行以下操作:

git获取和git签出您的branch_name

还为您创建了下图,以简化我所说的内容:

其他回答

从本地回购中克隆不能与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

所有分支机构

从Git项目下载所有分支的脚本

安装:

sudo git clone https://github.com/marceloviana/allBranches.git && sudo cp -rfv allBranches/allBranches.sh /usr/bin/allBranches && sudo chmod +x /usr/bin/allBranches && sudo rm -rf allBranches

准备好的现在只需调用命令(allBranches)并告诉Git项目目录,您希望下载所有分支

Use

示例1:

~$ allBranches /var/www/myproject1/

示例2:

~$ allBranches /var/www/myproject2/

示例3(如果已经在项目目录中):

~$ allBranches ./

or

~$ allBranches .

查看结果:

git branch

参考:

存储库allBranches GitHub:https://github.com/marceloviana/allBranches

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

我也需要做同样的事情。这是我的Ruby脚本。

#!/usr/bin/env ruby

local = []
remote = {}

# Prepare
%x[git reset --hard HEAD]
%x[git checkout master] # Makes sure that * is on master.
%x[git branch -a].each_line do |line|
  line.strip!
  if /origin\//.match(line)
     remote[line.gsub(/origin\//, '')] = line
   else
     local << line
   end
end
# Update 
remote.each_pair do |loc, rem|
  next if local.include?(loc)
  %x[git checkout --track -b #{loc} #{rem}]
end
%x[git fetch]

您只需使用“gitclone”即可获得所有分支。

git clone <your_http_url>

即使您只看到主分支,也可以使用“gitbranch-a”查看所有分支。

git branch -a

你可以切换到你已经拥有的任何分支。

git checkout <your_branch_name>

不要担心,在“gitclone”之后,您不需要连接到远程存储库。当您没有Internet连接时,可以成功运行“git branch-a”和“git checkout<your_branch_name>”。因此,事实证明,当您执行“gitclone”时,它已经从远程存储库复制了所有分支。之后,您就不需要远程存储库了。您的本地已具有所有分支机构的代码。