我克隆了一个包含许多分支的Git存储库。然而,git分支仅显示一个:
$ git branch
* master
我如何在本地拉所有分支,所以当我执行git分支时,它会显示以下内容?
$ git branch
* master
* staging
* etc...
我克隆了一个包含许多分支的Git存储库。然而,git分支仅显示一个:
$ git branch
* master
我如何在本地拉所有分支,所以当我执行git分支时,它会显示以下内容?
$ git branch
* master
* staging
* etc...
当前回答
如何获取跟踪单个远程的所有Git分支。
这已经在Windows 10上的Red Hat和Git Bash上进行了测试和运行。
TLDR:
for branch in `git branch -r|grep -v ' -> '|cut -d"/" -f2`; do git checkout $branch; git fetch; done;
说明:
一行程序检查,然后获取除HEAD之外的所有分支。
列出远程跟踪分支。
git branch -r
忽略HEAD。
grep -v ' -> '
删除远程的分支名称。
cut -d"/" -f2
签出跟踪单个远程的所有分支。
git checkout $branch
提取已签出的分支。
git fetch
从技术上讲,新的本地分支不需要获取。
这可以用于获取或拉取新的分支,这些分支在远程中都有更改。
只需确保只有在准备合并时才进行拉。
测试设置
使用SSH URL检查存储库。
git clone git@repository.git
之前
检查本地分支机构。
$ git branch
* master
执行命令
执行一行。
for branch in `git branch -r|grep -v ' -> '|cut -d"/" -f2`; do git checkout $branch; git fetch; done;
之后
检查本地分支是否包括远程分支。
$ git branch
cicd
master
* preprod
其他回答
为避免出现错误消息“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 clone --mirror repo.git .git (gets just .git - bare repository)
git config --bool core.bare false
git reset --hard
我写了一个小脚本来管理克隆一个新的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 branch -r | grep -v HEAD | awk -F'/' '{print $2 " " $1"/"$2}' | xargs -L 1 git branch -f --track
之后:
git fetch --all
git pull --all
$ git remote update
$ git pull --all
这假设所有分支都被跟踪。
如果他们不是,你可以在Bash中发射这个:
for remote in `git branch -r `; do git branch --track $remote; done
然后运行命令。
注意:请阅读下面的警告注释。