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

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

问题描述

我克隆了一个Git存储库,其中包含大约五个分支。但是,当我执行 git branch 时,我只会看到其中之一:

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

$ git branch
* master

我知道我可以做到 git branch -a 查看所有分支,但是当我执行 git branch ,它显示以下内容吗?

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 the following?

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


推荐答案

您可以提取所有像这样从所有远程站点分支:

You can fetch all branches 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. 获取不会更新本地分支(跟踪远程分支);如果要更新本地分支,则仍然需要拉出每个分支。

  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.

获取不会创建本地分支机构(该分支机构 远程分支),则必须手动执行此操作。如果要列出所有远程分支:
git branch -a

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

但是,这仍然不够。它仅适用于跟踪远程分支机构的本地分支机构。要跟踪所有远程分支,请执行此oneliner 之前 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版本



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

(看来pull会从所有远程获取所有分支,但我总是先获取来确保。)

(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.

PS AFAIK git fetch --all git remote update 是等效的。

P.S. AFAIK git fetch --all and git remote update are equivalent.

Kamil Szot的评论,人们发现这很有用。

Kamil Szot's comment, which folks have found useful.


我必须使用:

I had to use:

for remote in `git branch -r`; do git branch --track ${remote#origin/} $remote; done

因为您的代码创建了名为 origin / branchname
每当我
引用它时,我都会得到 refname'origin / branchname'是模糊的。

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天全站免登陆