我克隆了一个包含许多分支的Git存储库。然而,git分支仅显示一个:

$ git branch
* master

我如何在本地拉所有分支,所以当我执行git分支时,它会显示以下内容?

$ git branch
* master
* staging
* etc...

当前回答

您可以通过以下方式获取所有分支:

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复制存储库文章。

其他回答

循环似乎对我不起作用,我想忽略起源/主。这是对我有用的。

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
|‾‾‾‾‾‾‾‾‾‾‾‾‾fetch/clone‾‾‾‾‾‾‾‾‾‾‾‾↓   |‾‾‾‾‾‾‾‾‾‾‾‾checkout‾‾‾‾‾‾‾‾‾‾↓   
|‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾pull‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾↓
Remote repository (`origin`) <=> Local repository <=> Index <=> Workspace
↑_________________push_______________|   ↑____commit____|  ↑____add_____| 

# 拉取远程仓库所有分支信息 → 本地仓库
# fetch all remote repository branch meta → local repository
git remote set-branches origin '*'
git fetch -v

# 把所有远程分支数据搞到本地
# fetch all remote repository branch data → local repository
git branch -r | grep -v '\->' | while read remote; do git branch "${remote#origin/}" "$remote"; done
git fetch --all
git pull --all

您可以通过以下方式获取所有分支:

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复制存储库文章。

下面是接受答案中提供的一行程序的Perl版本:

git branch-r | perl-e'while(<>){chop;my$remote=$_;my($local)=($remote=~/origin\/(.*)/);打印“git branch--track$local$remote\n”;}'>一些输出文件

如果愿意,可以将输出文件作为Shell脚本运行。

我们无意中删除了Stash项目存储库。幸运的是,就在意外损失之前,有人制造了一个叉子。我将叉子克隆到了我的本地(将省略我是如何做到这一点的细节)。当我把叉子完全放在本地时,我跑了一条单行道。我修改了远程服务器的URL(在我的例子中是源URL),以指向我们要恢复到的目标存储库:

gitremoteseturl-origin<remoteurl>

最后将所有分支都推到原点,如下所示:

git push—所有原点

我们又回到了生意上。

根据Learnath2的回答,下面是我在执行gitclone[…]并cd到创建的目录后所做的操作:

git branch-r | grep-v master | awk{print\$1}| sed的/^ origin\/\(.*\)$/\1&/'| xargs-n2 git checkout-b

为我工作,但我不知道它会为你工作。小心。