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


当前回答

截至2017年初,该评论中的答案有效:

gitfetch<origin name><branch name>为您关闭分支。虽然这不会同时拉动所有分支,但您可以对每个分支单独执行此操作。

其他回答

在这里,我为您编写了一个很好的函数,使其易于重复

gitCloneAllBranches() { # clone all git branches at once easily and cd in
  # clone as "bare repo"
  git clone --mirror $1
  # rename without .git extension
  with_extension=$(basename $1)
  without_extension=$(echo $with_extension | sed 's/.git//')
  mv $with_extension $without_extension
  cd $without_extension
  # change from "bare repository" to not
  git config --bool core.bare false
  # check if still bare repository if so
  if [[ $(git rev-parse --is-bare-repository) == false ]]; then
    echo "ready to go"
  else
    echo "WARNING: STILL BARE GIT REPOSITORY"
  fi
  # EXAMPLES:
  # gitCloneAllBranches https://github.com/something/something.git
}

所有分支机构

从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

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

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

当您执行“gitclone”时git://location“,将获取所有分支和标记。

为了在特定远程分支上工作,假设它是源远程分支:

git checkout -b branch origin/branchname

这是一个使用awk的答案。如果在新的回购协议中使用,这种方法就足够了。

git branch -r | awk -F/ '{ system("git checkout " $NF) }'

现有的分支将被简单地签出,或者声明为已经在其中,但是可以添加过滤器以避免冲突。

还可以修改它,以便调用显式git checkout-b<branch>-t<remote>/<branch>命令。

这个答案符合尼科斯·C的想法。


或者,我们可以指定远程分支。这是基于murphytalk的答案。

git branch -r | awk '{ system("git checkout -t " $NF) }'

它会对冲突发出致命的错误消息,但我认为它们是无害的。


这两个命令都可以别名。

使用nobody的答案作为参考,我们可以使用以下命令来创建别名:

git config --global alias.clone-branches '! git branch -r | awk -F/ "{ system(\"git checkout \" \$NF) }"'
git config --global alias.clone-branches '! git branch -r | awk "{ system(\"git checkout -t \" \$NF) }"'

就我个人而言,我会使用跟踪所有分支或跟踪所有分支。