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


当前回答

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

其他回答

这个Bash脚本帮助了我:

#!/bin/bash
for branch in $(git branch --all | grep '^\s*remotes' | egrep --invert-match '(:?HEAD|master)$'); do
    git branch --track "${branch##*/}" "$branch"
done

它将为所有远程分支创建跟踪分支,除了master(您可能从原始clone命令中得到)。我想你可能还需要

git fetch --all
git pull --all

当然

一行:gitbranch-a|grep-vHEAD|perl-ne'chomp($_);s |^\*?\s*||;如果(m|(.+)/(.+)|&&not$d{$2}){print qq(git branch--track$2$1/$2\n)}否则{$d{$_}=1}‘|csh-xfs像往常一样:在复制rm-rf宇宙之前,在您的设置中进行测试一行的积分归用户cfi

从本地回购中克隆不能与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主机(github/bitbucket/etc)中的所有分支+引用+标记+等的“完整”备份,请运行:

mkdir -p -- myapp-mirror
cd myapp-mirror
git clone --mirror https://git.myco.com/group/myapp.git .git
git config --bool core.bare false
git config --bool core.logAllRefUpdates true
git reset --hard # restore working directory

这是根据我从其他答案中学到的一切整理而成的。

然后,您可以使用此本地回购镜像转换到不同的SCM系统/git主机,也可以将其作为备份。它作为搜索工具也很有用,因为大多数git主机只搜索每个repo的“main”分支上的代码,如果您使用git log-s“specialVar”,您将看到所有分支上的所有代码。

注意:如果您想在日常工作中使用此回购,请运行:

git config --unset remote.origin.mirror

警告:如果你试图在日常工作中使用它,你可能会遇到奇怪的问题。如果您的ide/编辑器正在进行一些自动提取,您的本地主控器可能会更新,因为您做了gitclone--mirror。然后这些文件会出现在git暂存区。事实上,我有一种情况,我在当地的一个特色分支。。该分支没有提交,repo中的所有文件都显示在暂存区域中。只是疯子。

我也需要做同样的事情。这是我的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]

以下是实现此目的的最佳方法:

mkdir repo
cd repo
git clone --bare path/to/repo.git .git
git config --unset core.bare
git reset --hard

此时,您已经拥有了远程存储库及其所有分支的完整副本(使用git分支进行验证)。如果远程存储库有自己的远程设备,则可以使用--mirror而不是--bare。