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


当前回答

我编写了这些小PowerShell函数,以便能够签出我的所有Git分支,这些分支位于源远程。

Function git-GetAllRemoteBranches {
     iex "git branch -r"                       <# get all remote branches #> `
     | % { $_ -Match "origin\/(?'name'\S+)" }  <# select only names of the branches #> `
     | % { Out-Null; $matches['name'] }        <# write does names #>
}


Function git-CheckoutAllBranches {
    git-GetAllRemoteBranches `
        | % { iex "git checkout $_" }          <# execute ' git checkout <branch>' #>
}

更多的Git函数可以在我的Git设置库中找到。

其他回答

要创建存储在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中的所有文件都显示在暂存区域中。只是疯子。

所有分支机构

从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

我认为这样做很有效:

mkdir YourRepo
cd YourRepo
git init --bare .git                       # create a bare repo
git remote add origin REMOTE_URL           # add a remote
git fetch origin refs/heads/*:refs/heads/* # fetch heads
git fetch origin refs/tags/*:refs/tags/*   # fetch tags
git init                                   # reinit work tree
git checkout master                        # checkout a branch

到目前为止,这对我有效。

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

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

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

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

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