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

$ git branch
* master

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

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

当前回答

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

其他回答

Bash for循环不适合我,但这正是我想要的。我的所有分支在本地镜像为相同的名称。

git checkout --detach
git fetch origin '+refs/heads/*:refs/heads/*'

请参见下面Mike DuPont的评论。我想我是在Jenkins服务器上做这件事的,这让它处于分离头部模式。

我们可以将所有分支或标记名称放在一个临时文件中,然后对每个名称/标记执行git pull:

git branch -r | grep origin | grep -v HEAD| awk -F/ '{print $NF}' > /tmp/all.txt
git tag -l >> /tmp/all.txt
for tag_or_branch in `cat /tmp/all.txt`; do git checkout $tag_or_branch; git pull origin $tag_or_branch; done

玩git时要小心,循序渐进。

$ git remote update  //This will update your local
$ git branch -a      //This will list all the branches(After updating you can now 
see the new branch in the list)
$ git checkout your_branch_name

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

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

只有这三个命令才能获得所有分支:

git clone --mirror repo.git  .git     (gets just .git  - bare repository)

git config --bool core.bare false

git reset --hard