如何将本地git仓库中的所有远程分支转换为本地跟踪分支 [英] How can I convert all the remote branches in a local git repo into local tracking branches
问题描述
如何将本地git仓库中的所有远程分支转换为本地跟踪分支,而不是逐个检查每个分支。
想要做到这一点(我想这样做的原因)是,你可以获得本地回购的克隆,并在新的克隆中从原始的远程原产地获得所有分支。
因为克隆只克隆本地分支。
编辑:已经提供了几个脚本答案(为此 - 谢谢!)...我真的希望采用一种合法的方式,所以它是完全可移植的(我拥有仅限Windows的用户,并且迄今为止无需使用bash(git-bash或其他方式)就可以存活)。
最好的方法是使用脚本:
#!/ bin / bash
IFS = $'\\\
'
用于`git branch -r`中的分支;如果[[$ {branch} =〜^ \ *(。+)/(。+)$]];则执行
;然后
git show-branch$ {BASH_REMATCH [2]}> / dev / null 2>& 1
if [$? -ne 0];然后
git branch $ {BASH_REMATCH [2]} $ {BASH_REMATCH [1]} / $ {BASH_REMATCH [2]}
fi
fi
完成
How can I convert all the remote branches in a local git repo into local tracking branches, without one by one checking each one out.
One reason why you might want to do this (the reason I want to do this) is so that you can take a clone of the local repo and have in that new clone all the branches from the original remote origin.
Because "clone" only clones local branches.
Edit: a couple of scripted answers have been provided (for which - thanks!) ... I was really hoping for an in-git way, so that it is completely portable (I have users who are "windows only", and so far have survived without having to use a bash (git-bash or otherwise)).
The best way to do this probably is with a script:
#!/bin/bash
IFS=$'\n'
for branch in `git branch -r`; do
if [[ ${branch} =~ ^\ *(.+)/(.+)$ ]]; then
git show-branch "${BASH_REMATCH[2]}" > /dev/null 2>&1
if [ $? -ne 0 ]; then
git branch ${BASH_REMATCH[2]} ${BASH_REMATCH[1]}/${BASH_REMATCH[2]}
fi
fi
done
这篇关于如何将本地git仓库中的所有远程分支转换为本地跟踪分支的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!