Branches and remotes
Asked by J Hite:

I seem to have a misunderstanding about branches and remotes. In a tabbed column on the left I see several blue branches which I have been told are my local working branches. I was working on branch bugFix_FFT, which I had just committed and pushed to origin.

Further down there are grey branches under Remotes including releaseV4.x. which I need to merge into my branch. I did not have a working copy of releaseV4.x and was about to checkout a new branch. However my co-worker said I must first perform a fetch to update releaseV4.x before I created a branch from it. I thought that the branches under remote would already be up to date since they are in the bare repository but he said that the grey releaseV4.x was what my "view" thought was the current version and needed to be updated with a fetch.

So was the fetch necessary?

Thanks,

jh


Answers:
author: Seth
body: Short answer: yes.

Explanation: Branches are just lightweight pointers to commits. Each time you fetch, **all** new commits in the repository are added to your local repository (regardless of whether they are part of any of your local branches), and your local record is updated regarding which commits the remote branches are pointing to.

This is one of the concepts that is completely different between Git/Hg vs centralized versioning like SVN. When you checkout a remote branch to your working copy in Git/Hg (making a local branch in the process), your repo does NOT need to access the remote to download the commits for that branch, because they are already in your local repo.

comments: author: J Hite
body: Thanks, but still not quite clear.  

You stated "Each time you fetch, \*all\* new commits in the repository are  
added to your local repository". I agree that it updates my local  
repositiory but my coworker said I needed to fetch to update the version of  
releaseV4.x under remotes before I checked it out into a new branch on my  
local. I was assuming that being under remotes meant it was not local but  
at origin and fetch would only update my local copies.  

Thanks,  
jh


author: Seth
body: Your assumption is a bit off. A branch being under remotes doesn't mean that it is not local. Rather, it means it doesn't belong to your local repository - you cannot commit directly to it. If you check it out, you create a new local branch that is configured to "track" origin's branch, and those two branches are kept in sync locally by merging (possibly fast-forward) or rebasing.Hope that helps.


author: J Hite
body: Thanks again.

I ain't the sharpest knife in the drawer but maybe my assumptions would be better if there was a user's manual.


author: Seth
body: You haven't actually specified which you use, but for Git you might check out the Git book: <http://git-scm.com/book/en/v2>


author: Bal?zs Szakm?ry
body: One addition: if you just need to merge an origin/ branch into your own, you don't need to check it out first. You can merge the origin branch into your branch. (After a fetch to make sure the origin branch reference is up-to-date.)


author: Seth
body: I tried that with SourceTree, and its UI doesn't give you the option.


author: Bal?zs Szakm?ry
body: Yes, it does:- Make sure you have "all branches" and "show remote branches" is on in the main view.- Find the commit with the right flag (e.g. origin/master), right-click, merge.(There is no context-menu item if you right-click the branch name in the origin branch list, though.)


author: Seth
body: Hmm, good point. I definitely only tried the branch list.