Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
505 views
in Technique[技术] by (71.8m points)

git - 如何获取所有Git分支(How to fetch all Git branches)

I cloned a Git repository, which contains about five branches.

(我克隆了一个Git存储库,它包含大约五个分支。)

However, when I do git branch I only see one of them:

(但是,当我做git branch我只看到其中一个:)

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

(我知道我可以做git branch -a来查看所有分支,但是我如何在本地拉出所有分支,所以当我做git branch ,它会显示以下内容?)

$ git branch
* master
* staging
* etc...
  ask by David542 translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can fetch all branches from all remotes like this:

(您可以从所有遥控器中获取所有分支,如下所示:)

git fetch --all

It's basically a power move .

(这基本上是一种权力移动 。)

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

(fetch更新远程分支的本地副本,因此这对于您的本地分支来说总是安全的但是 :)

  1. fetch will not update local branches (which track remote branches);

    (fetch不会更新本地分支( 跟踪远程分支);)

    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.

    (fetch不会创建本地分支( 跟踪远程分支),您必须手动执行此操作。)

    If you want to list all remote branches: git branch -a

    (如果要列出所有远程分支: 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 :

(要跟踪所有远程分支,请执行此oneliner BEFORE git pull --all :)

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

TL;DR version (TL; DR版本)

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.

(仅当服务器上存在未由本地分支跟踪的远程分支时,才运行第一个命令。)

PS AFAIK git fetch --all and git remote update are equivalent.

(PS AFAIK git fetch --allgit remote update是等效的。)



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 

because your code created local branches named origin/branchname and I was getting "refname 'origin/branchname' is ambiguous whenever I referred to it.

(因为你的代码创建了名为origin/branchname本地分支,而且每当我提到它时,我得到的“refname”origin / branchname'都是模棱两可的。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...