我克隆了一个包含许多分支的Git存储库。然而,git分支仅显示一个:
$ git branch
* master
我如何在本地拉所有分支,所以当我执行git分支时,它会显示以下内容?
$ git branch
* master
* staging
* etc...
我克隆了一个包含许多分支的Git存储库。然而,git分支仅显示一个:
$ git branch
* master
我如何在本地拉所有分支,所以当我执行git分支时,它会显示以下内容?
$ git branch
* master
* staging
* etc...
当前回答
Bash for循环不适合我,但这正是我想要的。我的所有分支在本地镜像为相同的名称。
git checkout --detach
git fetch origin '+refs/heads/*:refs/heads/*'
请参见下面Mike DuPont的评论。我想我是在Jenkins服务器上做这件事的,这让它处于分离头部模式。
其他回答
确保所有远程分支都可以在.git/config文件中获取。
在本例中,只有原始/生产分支是可获取的,即使您尝试执行git fetch,除了获取生产分支之外,一切都不会发生:
[origin]
fetch = +refs/heads/production:refs/remotes/origin/production
该行应替换为:
[origin]
fetch = +refs/heads/*:refs/remotes/origin/*
然后运行git fetch等。。。
我写了一个小脚本来管理克隆一个新的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 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
对于使用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
只有这三个命令才能获得所有分支:
git clone --mirror repo.git .git (gets just .git - bare repository)
git config --bool core.bare false
git reset --hard