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

$ git branch
* master

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

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

当前回答

您需要创建跟踪远程分支的本地分支。

假设您只有一个名为origin的远程,此代码片段将为所有远程跟踪分支创建本地分支:

for b in `git branch -r | grep -v -- '->'`; do git branch --track ${b##origin/} $b; done

之后,gitfetch-all将更新远程分支的所有本地副本。

此外,gitpull-all将更新您的本地跟踪分支,但根据您的本地提交以及“合并”配置选项的设置方式,它可能会创建一个合并提交、快进或失败。

其他回答

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

$ 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/config文件中获取。

在本例中,只有原始/生产分支是可获取的,即使您尝试执行git fetch,除了获取生产分支之外,一切都不会发生:

[origin]
fetch = +refs/heads/production:refs/remotes/origin/production

该行应替换为:

[origin]
fetch = +refs/heads/*:refs/remotes/origin/*

然后运行git fetch等。。。

我写了一个小脚本来管理克隆一个新的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>

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

|‾‾‾‾‾‾‾‾‾‾‾‾‾fetch/clone‾‾‾‾‾‾‾‾‾‾‾‾↓   |‾‾‾‾‾‾‾‾‾‾‾‾checkout‾‾‾‾‾‾‾‾‾‾↓   
|‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾pull‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾↓
Remote repository (`origin`) <=> Local repository <=> Index <=> Workspace
↑_________________push_______________|   ↑____commit____|  ↑____add_____| 

# 拉取远程仓库所有分支信息 → 本地仓库
# fetch all remote repository branch meta → local repository
git remote set-branches origin '*'
git fetch -v

# 把所有远程分支数据搞到本地
# fetch all remote repository branch data → local repository
git branch -r | grep -v '\->' | while read remote; do git branch "${remote#origin/}" "$remote"; done
git fetch --all
git pull --all

设置别名:(基于顶部答案)

git config --global alias.track-all-branches '!git fetch --all && for remote in `git branch -r`; do git branch --track ${remote#origin/} $remote; done && git fetch --all'

现在要跟踪所有分支:

git跟踪所有分支