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


当前回答

我正在用Python和Pytest从Udemy课程Elegant Automation Frameworks中克隆一个存储库,以便以后可以离线查看。我尝试下载zip,但这只适用于当前分支,因此这是我的2美分。

我在Windows上工作,显然,我求助于Windows Subsystem for Linux中的Ubuntu shell。克隆完成后,我的分支如下:

$ git clone https://github.com/BrandonBlair/elegantframeworks.git

$ git branch -a

* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/config_recipe
  remotes/origin/functionaltests
  remotes/origin/master
  remotes/origin/parallel
  remotes/origin/parametrize
  remotes/origin/parametrize_data_excel
  remotes/origin/unittesting
  remotes/origin/unittesting1

然后,在撞了几块几位数的结账砖墙之后,我终于明白了:

$ for b in `git branch -a | cut -c18- | cut -d\  -f1`; do git checkout $b; git stash; done

在此之后,以下是我的分支:

$ git branch -a

  config_recipe
  functionaltests
  master
  parallel
  parametrize
  parametrize_data_excel
  unittesting
* unittesting1
  remotes/origin/HEAD -> origin/master
  remotes/origin/config_recipe
  remotes/origin/functionaltests
  remotes/origin/master
  remotes/origin/parallel
  remotes/origin/parametrize
  remotes/origin/parametrize_data_excel
  remotes/origin/unittesting
  remotes/origin/unittesting1

我的是物理的,去掉了最初的遥控器/原点,然后过滤空间分隔符。可以说,我本可以把HEAD去掉,只剪一刀,但我会留给评论。

请注意,您当前的分支机构现在是列表中的最后一个。如果你不知道这是为什么,那你的处境就很艰难。现在你想要什么就结账吧。

其他回答

下面是根据前面的答案改编的跨平台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,它总是让您在尝试时感到困扰。

使用我的工具git_remote_branch(grb)。您需要在机器上安装Ruby)。它是专门为使远程分支操作非常容易而构建的。

每次它代表您执行操作时,都会在控制台上以红色打印。随着时间的推移,它们最终会进入你的大脑:-)

如果您不希望grb代表您运行命令,请使用“解释”功能。这些命令将打印到您的控制台,而不是为您执行。

最后,所有命令都有别名,以便于记忆。

注意,这是alpha软件;-)

以下是运行grb帮助时的帮助:

git_remote_branch version 0.2.6

  Usage:

  grb create branch_name [origin_server]

  grb publish branch_name [origin_server]

  grb rename branch_name [origin_server]

  grb delete branch_name [origin_server]

  grb track branch_name [origin_server]



  Notes:
  - If origin_server is not specified, the name 'origin' is assumed
    (git's default)
  - The rename functionality renames the current branch

  The explain meta-command: you can also prepend any command with the
keyword 'explain'. Instead of executing the command, git_remote_branch
will simply output the list of commands you need to run to accomplish
that goal.

  Example:
    grb explain create
    grb explain create my_branch github

  All commands also have aliases:
  create: create, new
  delete: delete, destroy, kill, remove, rm
  publish: publish, remotize
  rename: rename, rn, mv, move
  track: track, follow, grab, fetch

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

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
}

使用您可以记住的命令

我正在使用Bitbucket,一个Atlassian的存储库托管服务。所以我试着遵循他们的文档。这对我来说非常有用。使用以下简单而简短的命令,您可以签出远程分支。

首先克隆存储库,然后转到目标文件夹。最后,但并非最不重要的是,取件和结账:

git clone <repo> <destination_folder>
cd <destination_folder>
git fetch && git checkout <branch>

就是这样。这里有一个更真实的例子:

git clone https://username@bitbucket.org/team/repository.git project_folder
cd project_folder
git fetch && git checkout develop

您将在文档中找到有关命令的详细信息:克隆命令、获取命令、签出命令

#!/bin/bash
for branch in `git branch -a | grep remotes | grep -v HEAD | grep -v master `; do
   git branch --track ${branch#remotes/origin/} $branch
done

这些代码将把所有远程分支代码拉到本地存储库。