我克隆了一个包含许多分支的Git存储库。然而,git分支仅显示一个:
$ git branch
* master
我如何在本地拉所有分支,所以当我执行git分支时,它会显示以下内容?
$ git branch
* master
* staging
* etc...
我克隆了一个包含许多分支的Git存储库。然而,git分支仅显示一个:
$ git branch
* master
我如何在本地拉所有分支,所以当我执行git分支时,它会显示以下内容?
$ git branch
* master
* staging
* etc...
当前回答
我写了一个小脚本来管理克隆一个新的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>
它照常克隆,但为所有远程分支创建本地跟踪分支。
其他回答
为避免出现错误消息“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
如果您这样做:
git fetch origin
然后他们都会在当地。如果随后执行以下操作:
git branch -a
您将看到它们列为remotes/origin/分支名称。因为他们在当地,你可以随心所欲地和他们在一起。例如:
git diff origin/branch-name
or
git merge origin/branch-name
or
git checkout -b some-branch origin/branch-name
$ git remote update
$ git pull --all
这假设所有分支都被跟踪。
如果他们不是,你可以在Bash中发射这个:
for remote in `git branch -r `; do git branch --track $remote; done
然后运行命令。
注意:请阅读下面的警告注释。
尝试了很多方法,只有这一个简单,对我有用。
for branch in $(git ls-remote -h git@<your_repository>.git | awk '{print $2}' | sed 's:refs/heads/::')
do
git checkout "$branch"
git pull
done
您可以通过以下方式获取所有分支:
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复制存储库文章。