Git Pull Doesn't Do A Git Fetch

前端 未结 1 872
星月不相逢
星月不相逢 2020-12-17 23:09

My understanding has always been that git pull is essentially a combination of git fetch and git merge ... but I have encountered it a

相关标签:
1条回答
  • 2020-12-17 23:39

    This is a common source of confusion, so much so that the #git IRC channel has a canned help text for it, called !pull4:

    We recommend against using 'git fetch/pull <remote> <refspec>' (i.e. with branch argument), because it doesn't update the <remote>/<branch> ref. The easy way to fetch things properly is to get everything: 'git fetch' or 'git pull' are sufficient if you have one remote; otherwise we recommend 'git fetch <remote>' (plus 'git merge <remote>/<branch>' if you wanted to pull/merge).

    What's happening here is that git pull with no arguments does a git fetch on the "default remote" (typically origin), then git merges the remote branch corresponding to your current local branch. git pull <remote> does the same thing with an explicitly specified remote. However, the "four-word pull", git pull <remote> <branch>, fetches that branch into a temporary location, FETCH_HEAD, without updating your tracking branches, and then merges FETCH_HEAD into your current branch.

    So, by using git pull origin blob, you are saying "I want to merge the latest version of the blob branch on the remote origin into my current branch, without updating any of my tracking branches or fetching any other data".

    If your branch merge configurations are correct (and they probably are, since git clone and git checkout try to set them up automatically), you can just git pull or git pull origin and it'll fetch everything and then merge the branch corresponding to your currently-checked-out branch. If you want more explicit control, use git fetch and then git merge. git pull <remote> <branch> is rarely what you want.

    (The reason the git fetch fixes things in your example is that, after the pull, your local branch has been updated with the latest remote commits, but the tracking branch hasn't been. git fetch updates the tracking branch with those commits, at which point the diffs start making sense again.)

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