git 从远程仓库拉取所有分支

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18891794/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 17:00:38  来源:igfitidea点击:

git pull all branches from remote repository

gitversion-controlgit-branchremote-branch

提问by MarcinWolny

How do I pull all of the remote branches to my own repository?

如何将所有远程分支拉到我自己的存储库中?

if I type:

如果我输入:

git branch -a

I get a long list of branches, but if I type:

我得到一长串分支,但如果我输入:

git branch 

I see only 2 of them.

我只看到其中的 2 个。

How do I pull ALLbranches into my local list?

如何将所有分支拉入我的本地列表?

I know I can do:

我知道我可以做到:

git checkout --track origin/branch-name

but that pulls and checks out only one branch at a time. Any way to get it all done at once without that whole tedious work of running git checkout --track origin/branch-nameover and over and over again?

但这一次只提取并检查一个分支。有什么方法可以一次完成所有工作,而无需git checkout --track origin/branch-name一遍又一遍地进行繁琐的工作?



ps. I tried following commands, none of them made remote branches appear in my git branchlist:

附:我尝试了以下命令,但没有一个使远程分支出现在我的git branch列表中:

git fetch --all
git remote update
git pull --all

采纳答案by VonC

The command I usually use to make all visible upstreambranches, and tracking themis detailed in "Track all remote git branches as local branches":

我通常用来使所有可见的上游分支和跟踪它们的命令在“将所有远程 git 分支跟踪为本地分支”中详细说明:

remote=origin ; for brname in `git branch -r | grep $remote | grep -v master | grep -v HEAD | awk '{gsub(/[^\/]+\//,"",); print }'`; do git branch --set-upstream-to $remote/$brname $brname ; done

Or:

或者:

remote=origin ; for brname in `git branch -r | grep $remote | grep -v master | grep -v HEAD | awk '{gsub(/[^\/]+\//,"",); print }'`; do git branch --track $brname $remote/$brname  ; done

For more readability:

为了提高可读性:

remote=origin ; // put here the name of the remote you want
for brname in `
  git branch -r | grep $remote | grep -v master | grep -v HEAD 
  | awk '{gsub(/[^\/]+\//,"",); print }'
`; do 
  git branch --set-upstream-to $remote/$brname $brname; 
  # or
  git branch --track $brname  $remote/$brname ; 
done

The second one is for creating new local branches tracking remote branches.

第二个是用于创建新的本地分支跟踪远程分支。

回答by Uwe Geuder

Read e.g. this explanation http://git-scm.com/book/en/Git-Branching-Remote-Branches

阅读例如这个解释http://git-scm.com/book/en/Git-Branching-Remote-Branches

First let's clarify some git terminology:

首先让我们澄清一些 git 术语:

  • fetch: getting contents (or updates) from a remote repo
  • pull: fetch (as above) and merge in one step
  • fetch:从远程仓库获取内容(或更新)
  • pull:获取(如上)并一步合并

The original poster did not mention merging, so I might guess in proper git terminology he might even have wanted to ask "git fetch all branches from remote repository"

最初的海报没有提到合并,所以我可能用正确的 git 术语猜测他甚至可能想问“git fetch all branch from remote repository”

If you see the branches in git branch -athen you have already fetched them. You can verify this by giving the command git show remotes/origin/some-branch:some-file

如果您看到分支,git branch -a那么您已经获取了它们。您可以通过给出命令来验证这一点git show remotes/origin/some-branch:some-file

Or can do e.g. git diff remotes/origin/some-branch master

或者可以做例如 git diff remotes/origin/some-branch master

You can even check them out git checkout remotes/origin/some-branch

你甚至可以查看它们 git checkout remotes/origin/some-branch

(To be sure you can remove the network cable and you will see that the commands work without contacting the remote repo.)

(为了确保您可以移除网络电缆,您将看到命令在不联系远程存储库的情况下工作。)

The branches named remotes/... are called remote branches, but they are already fetched to your repo. They are read-only, you cannot modify them (that's why a message appears when checking out). Although they reflect the state of a remote repo at the time of the last fetch or pull operation they are in fact stored locally.

名为 remotes/... 的分支称为远程分支,但它们已被提取到您的存储库中。它们是只读的,您不能修改它们(这就是为什么在签出时会出现一条消息)。尽管它们反映了上次提取或拉取操作时远程存储库的状态,但它们实际上存储在本地。

If you do git checkout some-branchand some-branch does not yet exist but remotes/origin/some-branch exists, git will create a tracking branch called some-branch for you (1). Again this is a local operation, all data has been fetched before (or if you have not recently fetched, you will start working on an obsolete version). Originally the contents of the tracking branch is identical to its remote branch. However, the tracking branch can be modified by you locally.

如果您这样做了git checkout some-branch并且 some-branch 尚不存在但 remotes/origin/some-branch 存在,则 git 将为您创建一个名为 some-branch 的跟踪分支 (1)。同样,这是一个本地操作,所有数据之前都已获取(或者如果您最近没有获取,您将开始处理过时的版本)。最初跟踪分支的内容与其远程分支相同。但是,您可以在本地修改跟踪分支。

The git working area contains the state of one branch at time. So your question about checking out all remote branches at once does not really make sense in the context of git. You can check out them one after each other. But each time you check out the next one, the previous one will disappear from the working area. Of course this operation can be scripted as shown in Track all remote git branches as local branchesBut what is the point of scripting a mass operation if only its last step is what remains?

git 工作区包含一个分支的状态。因此,您关于一次检查所有远程分支的问题在 git 上下文中并没有真正意义。您可以一个接一个地查看它们。但是每次检查下一个时,前一个都会从工作区中消失。当然,此操作可以编写脚本,如将所有远程 git 分支跟踪为本地分支中所示,但是如果只剩下最后一步,那么编写大规模操作脚本的意义何在?

So could the question be caused by a misunderstanding, assuming that remote branches would be only stored remotely, but not locally and you just wanted to make sure that you have everything local? If you really want to have more than one branch checked out at a time you can clone your repo locally and checkout different branches into different work areas. (2)

那么这个问题可能是由误解引起的,假设远程分支只会远程存储,而不是本地存储,而您只想确保所有内容都在本地?如果你真的想一次签出多个分支,你可以在本地克隆你的仓库并将不同的分支签出到不同的工作区。(2)

Shortly: If you want to be sure that you have all data available locally that is in the remote repo just use git fetch [repo]. Unless you have tweaked with your configuration this will fetch all branches, i.e. updating existing remote branches and also creating new remote ones if applicable.

很快:如果您想确保远程存储库中的所有本地数据都可用,只需使用git fetch [repo]. 除非您对配置进行了调整,否则这将获取所有分支,即更新现有的远程分支并在适用时创建新的远程分支。

(1) This is true in simple standard cases. In more complicated cases with more than 1 remote or manually configured remotes you might need the --track option to specify exactly what you want.

(1) 这在简单的标准情况下是正确的。在具有 1 个以上远程或手动配置的远程的更复杂的情况下,您可能需要 --track 选项来准确指定您想要的内容。

(2) There is a new feature git worktreefor this use case. However as of early 2018 it is still marked experimental

(2)git worktree此用例有一个新功能。然而,截至 2018 年初,它仍处于实验阶段

回答by pixel 67

I've always used this and it works perfectly

我一直在用这个,而且效果很好

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

回答by Mr. Pundir

You Should Try Something Like:-

你应该尝试这样的事情:-

$ git fetch --all
$ for branch in `git branch -r | cut -d '/' -f2-` ; do git checkout $branch && git pull origin $branch ; done