我克隆了一个包含许多分支的Git存储库。然而,git分支仅显示一个:

$ git branch
* master

我如何在本地拉所有分支,所以当我执行git分支时,它会显示以下内容?

$ git branch
* master
* staging
* etc...

当前回答

克隆存储库时,实际上会下载分支的所有信息,但分支是隐藏的。使用命令

$ git branch -a

您可以显示存储库的所有分支,并使用以下命令

$ git checkout -b branchname origin/branchname

然后,您可以一次手动下载一个。


然而,有一种更干净、更快的方法,尽管它有点复杂。您需要三个步骤来实现这一点:

第一步在计算机上创建一个新的空文件夹,并从存储库克隆.git文件夹的镜像副本:$cd~/桌面&&mkdir my_repo_folder&&cd my_repo-folder$git克隆--镜像https://github.com/planetoftheweb/responsivebootstrap.git.git位myepofolder文件夹中的本地存储库仍然是空的,现在只有一个隐藏的.git文件夹,您可以通过终端的“ls-alt”命令看到它。第二步通过将git配置的布尔值“裸”切换为false,将此存储库从空(裸)存储库切换为常规存储库:$git-config--bool core.bare false第三步获取当前文件夹中的所有内容,并在本地计算机上创建所有分支,因此这是一个正常的回购。$git重置--硬

现在您只需键入命令gitbranch,就可以看到所有的分支都已下载。

这是一种快速的方法,您可以一次克隆一个包含所有分支的git存储库,但这不是您想用这种方法为每个项目做的事情。

其他回答

克隆主存储库后,您只需执行

git fetch && git checkout <branchname>

使用git fetch和git checkout RemoteBranchName。

这对我来说很好。。。

对于使用PowerShell的Windows用户:

git branch -r | ForEach-Object {
    # Skip default branch, this script assumes
    # you already checked-out that branch when cloned the repo
    if (-not ($_ -match " -> ")) {
        $localBranch = ($_ -replace "^.*?/", "")
        $remoteBranch = $_.Trim()
        git branch --track "$localBranch" "$remoteBranch"
    }
}; git fetch --all; git pull --all

我用这个命令拉动原点分支,git拉动原点

我写了一个小脚本来管理克隆一个新的repo,并为所有远程分支创建本地分支。

您可以在此处找到最新版本:

#!/bin/bash

# Clones as usual but creates local tracking branches for all remote branches.
# To use, copy this file into the same directory your git binaries are (git, git-flow, git-subtree, etc)

clone_output=$((git clone "$@" ) 2>&1)
retval=$?
echo $clone_output
if [[ $retval != 0 ]] ; then
    exit 1
fi
pushd $(echo $clone_output | head -1 | sed 's/Cloning into .\(.*\).\.\.\./\1/') > /dev/null 2>&1
this_branch=$(git branch | sed 's/^..//')
for i in $(git branch -r | grep -v HEAD); do
  branch=$(echo $i | perl -pe 's/^.*?\///')
  # this doesn't have to be done for each branch, but that's how I did it.
  remote=$(echo $i | sed 's/\/.*//')
  if [[ "$this_branch" != "$branch" ]]; then
      git branch -t $branch $remote/$branch
  fi
done
popd > /dev/null 2>&1

要使用它,只需将其复制到git bin目录中(对我来说,这是C:\Program Files(x86)\git\bin\git cloneall),然后在命令行上:

git cloneall [standard-clone-options] <url>

它照常克隆,但为所有远程分支创建本地跟踪分支。