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

$ git branch
* master

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

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

当前回答

如果您在fetch-all方面遇到问题,请跟踪远程分支:

git checkout --track origin/%branchname%

其他回答

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

git branch -r

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

git checkout -b local_branch_name origin/remote_branch_name

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

为避免出现错误消息“fatal:名为“origin/master”的分支已存在。”,你可以试试我的解决方案:

git branch -r | grep -v '\->'  | grep -v `git branch | awk '/\*/ { print $2; }'`| while read remote; do git branch --track "${remote#origin/}" "$remote"; done

以下是我认为可靠的东西:

不更新现有分支的远程跟踪不尝试更新HEAD以跟踪原点/HEAD允许命名为非原点的远程正确引用外壳

for b in $(git branch -r --format='%(refname:short)'); do
  [[ "${b#*/}" = HEAD ]] && continue
  git show-ref -q --heads "${b#*/}" || git branch --track "${b#*/}" "$b";
done
git pull --all

不需要git fetch,因为将all传递给git pull会将此选项传递给内部fetch。

这个答案值得称赞。

使用git fetch和git checkout RemoteBranchName。

这对我来说很好。。。