git pull all branches from remote repository

故事扮演 提交于 2019-12-28 05:01:23

问题


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.

How do I pull ALL branches 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-name over and over and over again?


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

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

回答1:


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

remote=origin ; for brname in `git branch -r | grep $remote | grep -v master | grep -v HEAD | awk '{gsub(/[^\/]+\//,"",$1); print $1}'`; 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(/[^\/]+\//,"",$1); print $1}'`; 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(/[^\/]+\//,"",$1); print $1}'
`; 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.




回答2:


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

First let's clarify some git terminology:

  • fetch: getting contents (or updates) from a remote repo
  • pull: fetch (as above) and merge in one step

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"

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

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

You can even check them out 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.

If you do git checkout some-branch and 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.

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 branches But what is the point of scripting a mass operation if only its last step is what remains?

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)

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.

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

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




回答3:


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



回答4:


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


来源:https://stackoverflow.com/questions/18891794/git-pull-all-branches-from-remote-repository

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!