如何获取所有的git分支? [英] How to fetch all git branches?
问题描述
git branch
时,我只能看到其中一个: $ git branch
* master
我知道我可以做 git branch -a
来查看所有分支,但是我怎样才能在本地取所有分支,所以当我做 git branch $ c它显示:
$ $ p $ $ code $ git分支
$ master
$ st
* etc ...
前段时间写了我的答案,最后downvote激励我用我以后的经验更新它: - )
编辑:此答案的前一版本创建具有'origin'前缀的分支,全部指向主分支,而不是实际分支,并且存在变量扩展问题。这已经根据评论被修复。
您可以像这样从所有遥控器获取一个分支:
git fetch --all
fetch
更新远程分支的本地副本,所以这对您的本地分支机构始终是安全的但:
-
fetch
不会更新本地分支( track 远程分支);如果你想更新你的本地分行,你仍然需要拉动每一个分行。 远程分支),您必须手动执行此操作。如果你想列出所有的远程分支:
git branch -a
至更新追踪远程分行的本地分行:
git pull - -all
但是,这仍然不够。它只适用于追踪远程分行的当地分行。要追踪所有远程分支机构,请执行以下操作: BEFORE git pull --all
:
git branch -r | grep -v'\->'|同时阅读远程;做git branch --track$ {remote#origin /}$ remote;完成
TL; DR版本
git branch -r | grep -v'\->'|同时阅读远程;做git branch --track$ {remote#origin /}$ remote;完成
git fetch --all
git pull --all
似乎拉从所有遥控器获取所有分支,但我总是首先获取,以确保它)
仅当服务器上存在远程分支时才运行第一个命令不会被当地分行追踪。
PS AFAIK git fetch --all
和 git remote update
是等同的。
git branch -r | grep -v '\->' | while read remote; do git branch --track "${remote#origin/}" "$remote"; done
git fetch --all
git pull --all
(it seems that pull fetches all branches from all remotes, but I always fetch first just to be sure)
Run the first command only if there are remote branches on the server that aren't tracked by your local branches.
P.S. AFAIK git fetch --all
and git remote update
are equivalent.
Kamil Szot's comment, which 74 (at least) people found useful.
I had to use:
for remote in `git branch -r`; do git branch --track ${remote#origin/} $remote; done
because your code created local branches named
origin/branchname
and I was getting "refname 'origin/branchname' is ambiguous whenever I referred to it.
这篇关于如何获取所有的git分支?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!