我克隆了一个包含许多分支的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服务器上做这件事的,这让它处于分离头部模式。

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

我相信您已经通过以下方式克隆了存储库:

git clone https://github.com/pathOfrepository

现在使用cd转到该文件夹:

cd pathOfrepository

如果键入gitstatus,您可以看到所有:

   On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working directory clean

要查看所有隐藏的分支类型,请执行以下操作:

 git branch -a

它将列出所有远程分支。

现在,如果您想在任何特定分支上签出,只需键入:

git checkout -b localBranchName origin/RemteBranchName

我们可以将所有分支或标记名称放在一个临时文件中,然后对每个名称/标记执行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