git fetch vs. git fetch origin master have different effects on tracking branch

后端 未结 3 1532
广开言路
广开言路 2020-12-04 14:56

This is mostly of the nature of a curiosity as I\'m trying to get familiar with Git. I have looked at the documentation for \'git fetch\' but I don\'t see an obvious explana

相关标签:
3条回答
  • 2020-12-04 15:29

    The answer lies in the messages you get back from git fetch. In the first case, when you fetch without providing a refspec, you'll see that the remote tracking branches are updated:

    remote: Counting objects: 5, done.
    remote: Total 3 (delta 0), reused 0 (delta 0)
    Unpacking objects: 100% (3/3), done.
    From /depot
       c67d1c8..1941673  master     -> origin/master
    

    Note how the message says that origin/master is updated with the master from the origin.

    Now in the second case, where you specify the refspec, you get something altogether different:

    remote: Counting objects: 5, done.
    remote: Total 3 (delta 0), reused 0 (delta 0)
    Unpacking objects: 100% (3/3), done.
    From /depot
     * branch            master     -> FETCH_HEAD
    

    So when you specify the refspec, the remote tracking branch (origin/master) is NOT updated, only FETCH_HEAD.

    The end result is that you'll appear to be ahead of origin/master when you're not really. I can't imagine why this behavior would be desirable, but it's definitely an interesting little quirk of the fetch command.

    0 讨论(0)
  • 2020-12-04 15:29

    If you want to fast forward merge yourself, or use git pull. You don't seem to understand that the purpose of git fetch is NOT to update your working tree. Fetch is meant to update your tracking branches.

    0 讨论(0)
  • 2020-12-04 15:32

    If your branch has an associated remote tracking branch that means its configuration is like:

    git config branch.[branch-name].remote [remote-name]
    git config branch.[branch-name].merge [remote-master]
    

    The key part of git fetch which explain the difference between the two commands is:

    <refspec>
    

    The format of a <refspec> parameter is an optional plus +, followed by the source ref <src>, followed by a colon :, followed by the destination ref <dst>.
    The remote ref that matches <src> is fetched, and if <dst> is not empty string, the local ref that matches it is fast-forwarded using <src>.

    Let me repeat it:

    if <dst> is not empty string, the local ref that matches it is fast-forwarded using <src>.
    Knowing that:

    • git fetch is equivalent to git fetch origin master:master (from the default value of your branch config), so it will update the remote tracking branch: the destination of the refspec is specified for you.

    • git fetch origin master is equivalent to "git fetch origin master:", not to "git fetch origin master:master"; it stores fetched value of 'master' branch (of remote 'origin') in FETCH_HEAD, and not in 'master' branch or remote-tracking 'remotes/origin/master' branch (from Jakub Narębski's answer)
      In other words, you didn't specify the destination of your refspec

    0 讨论(0)
提交回复
热议问题