Git fetch and pull operation on branches

This might have been asked before but I can not find the answer.

Assume following scenario.

I have a local repository with following local branches.

Scenario 1 

    Branch A
    Branch B
    Branch C (not reachable from A or B)

if I am on branch A and do a pull, do I implicitly do a fetch for B and C? Is the fetch done in pull operation different form just doing just doing fetch?

if I do fetch on C do I fetch the HEAD for A and B?

What about scenario two where only Branch A is checked out locally?

Scenario 2

    Branch A

    Branch A
    Branch B
    Branch C (not reachable from A or B)

  • How restrict only one pull request at a time in TFS git?
  • What is the difference between “git pull origin” and “git pull remote”?
  • Git pull doesn't know which branch to merge with
  • Is 'pull' a synonym for 'clone' in a Mercurial source-control repository?
  • pulling and integrating remote changes with pygit2
  • When will `git pull --rebase` get me in to trouble?
  • How to get modified files when I pull using NetBeans?
  • Force GIT Pull without commiting
  • One Solution collect form web for “Git fetch and pull operation on branches”

    • Your description of (and the difference between) the two scenarios is not quite clear , since if the remote has some branches names A, B, and C, it will always exist in your local repo as well as origin/A, origin/B and origin/C, (after you’ve updated the remote using git fetch or a git pull), irrespective of whether you’ve checked out the branch locally or not.

    • fetch is used to fetch the updated list of commits from a remote. fetch is done as the first command of a pull operation.

      The commonly syntax is git fetch <REMOTE> <REFSPEC>

      The refspec argument is optional. If you fail to specify one, git is going to pull all the refs from the remote. By refs, it includes all reachable heads and tags in the refspec.

    • The git pull <REMOTE> command without any refspecs is going to bring in all the refs from the remote, and NOT just the commits corresponding to your current local branch you’re merging into.

    • In Scenario 1, if you run a git pull origin when you’ve currently checked out branch A, yes git is going to update origin/A, origin/B, and origin/C in your local repo. In addition, it will merge origin/A into A.

    • In Scenario 1, if you run a git fetch origin C, only origin/C in your local repo is going to get updated. There will be no merges either whatsoever because of this fetch.

    • Scenario 2 is going to yield the same results as Scenario 1. All the refs from the remote will be updated into your local repo’s <REMOTE_NAME>/BLAH branches. In fact Scenario 2 is no different than Scenario 1, just that you have fewer local branches checked out which should not matter in any way for the fetch operation.

    BTW to answer your question in the comment on what git pull origin <BRANCHNAME> and git fetch origin <BRANCHNAME> do:

    • git pull origin <BRANCHNAME> does something different than what you might expect it to do.

      • git brings in the commits for <BRANCHNAME> from the remote into FETCH_HEAD instead of origin/<BRANCHNAME>.
      • It does not update origin/<BRANCHNAME>
      • It merges FETCH_HEAD into your currently checked out branch, and hence achieves the similar end result as far as the local branch state is concerned.
    • git fetch origin <BRANCHNAME> behaves very similar to git pull origin <BRANCHNAME> described above, except it stops with just bringing in the commits for <BRANCHNAME> from the remote into FETCH_HEAD.

    • If you really want to fetch only the commits corresponding to a particular branch, you need to use the right refspec argument. Read more on it here. To fetch only the commits from the remote origin and branch master, the correct command would be:

       git pull origin refs/heads/master:refs/remotes/origin/master
    • You could also use refspecs with fetch to only fetch the commits into origin/master without any merges into your local branches:

       git fetch origin refs/heads/master:refs/remotes/origin/master
    • If you do not want to type the long refspecs for a git pull, and do not mind having the info about all refs from the remote, and you do not have a remote tracking branch set, then you could just do this:

      git fetch origin
      git merge origin/<BRANCH-TO-BE-MERGED-FROM>

    BONUS – What does the GIT_FETCH_HEAD do ?

    It is a short-lived ref used by git to keep track of any fetches from the remote repositories. You could see the current value of FETCH_HEAD by looking into the file .git/FETCH_HEAD. For example:

    72a90cf95456b14630ddff427c3df1b00be24670                branch 'master' of git://
    Git Baby is a git and github fan, let's start git clone.