如何获取所有的git分支? [英] How to fetch all git branches?

查看:277
本文介绍了如何获取所有的git分支?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我克隆了一个包含大约5个分支的git仓库。然而,当我做 git branch 时,我只能看到其中一个:

  $ git branch 
* master

我知道我可以做 git branch -a 来查看所有分支,但是我怎样才能在本地取所有分支,所以当我做 git branch
$ $ p $ $ code $ git分支
$ master
$ st
* etc ...


解决方案

前段时间写了我的答案,最后downvote激励我用我以后的经验更新它: - )



编辑:此答案的前一版本创建具有'origin'前缀的分支,全部指向主分支,而不是实际分支,并且存在变量扩展问题。这已经根据评论被修复。



您可以像这样从所有遥控器获取一个分支:

  git fetch --all 

fetch 更新远程分支的本地副本,所以这对您的本地分支机构始终是安全的


  1. fetch 不会更新本地分支( track 远程分支);如果你想更新你的本地分行,你仍然需要拉动每一个分行。

  2. 远程分支),您必须手动执行此操作。如果你想列出所有的远程分支:
    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 是等同的。







Kamil Szot's


我有使用:

 用于`git branch -r`中的remote;做git分支--track $ {remote#origin /} $ remote;完成

因为您的代码创建了名为 origin / branchname
我得到的refname'origin / branchname'是不明确的,只要我
引用它。



I cloned a git repository, which contains about 5 branches. However, when I do git branch I only see one of them:

$ git branch
* master

I know that I can do git branch -a to see all the branches, but how would I pull all the branches locally so when I do git branch, it shows:

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

I've written my answer some time ago and last downvote motivated me to update it with my later experience :-)

Edit: previous version of this answer created branches with 'origin' prefix, all pointing to master branch, instead of actual branches, and having problems with variable expansions. This has been fixed as per comments.

You can fetch one branch from all remotes like this:

git fetch --all

fetch updates local copies of remote branches so this is always safe for your local branches BUT:

  1. fetch will not update local branches (which track remote branches); If you want to update your local branches you still need to pull every branch.

  2. fetch will not create local branches (which track remote branches), you have to do this manually. If you want to list all remote branches: git branch -a

To update local branches which track remote branches:

git pull --all

However, this can be still insufficient. It will work only for your local branches which track remote branches. To track all remote branches execute this oneliner BEFORE git pull --all:

git branch -r | grep -v '\->' | while read remote; do git branch --track "${remote#origin/}" "$remote"; done

TL;DR version

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆