Home > OS >  Does checking out a branch with git after doing git fetch still goto the origin?
Does checking out a branch with git after doing git fetch still goto the origin?

Time:09-22

If I have a remote server setup with my local repo and I do a git fetch for the first time, if there were 10 remote branches, I would now have 10 remote tracking branches from what I've read.

From what I've also gathered these remote tracking branches are pointers to those remote branches rather than actual full local copies of the code on those branches...is that correct?

My follow up then is if I was to checkout one of these remote branches after a git fetch, does git still need to connect to the remote to get all the code or would it simply switch to the remote tracking branch on the local machine which already has all the code?

CodePudding user response:

From what I've also gathered these remote tracking branches are pointers to those remote branches rather than actual full local copies of the code on those branches...is that correct?

No, it is not correct.

Git branches are just a name and a commit ID. There's nothing special about remote branches, except you don't commit to them.

When you fetch you download a full copy of every commit. Each commit knows which commit came before it (or in the case of a merge, two or more commits). This is the "history". Git is very efficient at storing and transmitting that history.

My follow up then is if I was to checkout one of these remote branches after a git fetch, does git still need to connect to the remote to get all the code or would it simply switch to the remote tracking branch on the local machine which already has all the code?

Git already has a complete copy of the repository. When you git checkout origin/main Git switches to the already downloaded commit that origin/main points at.

Only a handful of Git commands do any network access. The primary ones are git push, git fetch and git pull (which is just a fetch a merge).

  • Related