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
458 views
in Technique[技术] by (71.8m points)

version control - git pull all branches from remote repository

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
See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

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


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

...