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

$ git branch
* master

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

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

当前回答

对于使用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

其他回答

对于Visual Studio用户,在包管理器控制台上:

git branch | %{ git fetch upstream; git merge upstream/master}

您可以通过以下单行命令获取所有分支:

git fetch --all && git pull --all && git branch -r | grep -v '\->' | while read remote; do git branch --track "${remote#origin/}" "$remote"; done

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

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

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

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

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

您可以通过以下方式获取所有分支:

git fetch --all

or:

git fetch origin --depth=10000 $(git ls-remote -h -t origin)

如果您对存储库进行了浅处理,--depth=10000参数可能会有所帮助。


要拉动所有分支,请使用:

git pull --all

如果上面的命令不起作用,那么在上面的命令前面加上:

git config remote.origin.fetch '+refs/heads/*:refs/remotes/origin/*'

因为remote.origin.fetch在提取时只能支持特定的分支,特别是当您使用--single分支克隆回购时。通过:git-config remote.origin.fetch检查此项。

之后,您应该可以签出任何分支。

另请参见:

如何获取所有远程分支?如何克隆Git中的所有远程分支?


要将所有分支推送到远程,请使用:

git push --all

最终--镜像到镜像所有引用。


如果您的目标是复制存储库,请参阅:在GitHub复制存储库文章。

要列出远程分支,请执行以下操作:

git branch -r

要将远程分支签出为本地分支,请执行以下操作:

git checkout -b local_branch_name origin/remote_branch_name