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


当前回答

下面是根据前面的答案改编的跨平台PowerShell 7函数。

function Invoke-GitCloneAll($url) {
    $repo = $url.Split('/')[-1].Replace('.git', '')
    $repo_d = Join-Path $pwd $repo
    if (Test-Path $repo_d) {
        Write-Error "fatal: destination path '$repo_d' already exists and is not an empty directory." -ErrorAction Continue
    } else {
        Write-Host "`nCloning all branches of $repo..."
        git -c fetch.prune=false clone $url -q --progress &&
        git -c fetch.prune=false --git-dir="$(Join-Path $repo_d '.git')" --work-tree="$repo_d" pull --all
        Write-Host "" #newline
    }
}

注意:-c fetch.sprune=false使其包含通常会被排除的过时分支。如果你对它不感兴趣,就去掉它。


通过从函数中删除&&,可以在PowerShell 5.1(Windows 10中的默认值)中实现这一点,但这使得它即使在上一个命令失败时也会尝试git pull。因此,我强烈建议您只使用跨平台PowerShell,它总是让您在尝试时感到困扰。

其他回答

您只需使用“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”时,它已经从远程存储库复制了所有分支。之后,您就不需要远程存储库了。您的本地已具有所有分支机构的代码。

所有分支机构

从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

看着问题的答案之一,我注意到可以缩短它:

for branch in  `git branch -r | grep -v 'HEAD\|master'`; do
    git branch --track ${branch##*/} $branch;
done

但是要注意,如果其中一个远程分支被命名,例如admin_master,它将不会被下载!

独立存储库

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

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服务器上远程分支的更改。

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