Git Pull 不做 Git Fetch [英] Git Pull Doesn't Do A Git Fetch
问题描述
我的理解一直是 git pull
本质上是 git fetch
和 git merge ...
的组合但是我遇到过多次拉取,然后比较显示不属于我的更改,直到我也进行提取:
My understanding has always been that git pull
is essentially a combination of git fetch
and git merge ...
but I have encountered it a number of times where pulling, then comparing shows changes that aren't mine until I also do a fetch:
(在分支 blob 上):
(on branch blob):
git pull origin blob
git diff origin/blob <- shows a bunch of changes that aren't from my but were just pulled from others
git fetch
git diff origin/blob <- shows just my changes
我对 pull 的理解不正确吗?
Is my understanding of pull incorrect?
推荐答案
这是一个常见的混淆来源,以至于 #git
IRC 频道有一个罐头帮助文本,称为!pull4
:
This is a common source of confusion, so much so that the #git
IRC channel has a canned help text for it, called !pull4
:
我们建议不要使用 'git fetch/pull <remote> <refspec>'(即带有分支参数),因为它不会更新 <remote>/<branch> 参考.正确获取东西的简单方法是获取所有内容:如果您有一个遥控器,git fetch"或git pull"就足够了;否则我们推荐 'git fetch <remote>' (如果你想拉/合并,加上 'git merge <remote>/<branch>').
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).
这里发生的是 git pull
没有参数在默认远程"(通常是 origin
)上执行 git fetch
,然后git merge
是你当前本地分支对应的远程分支.git pull <remote>
对明确指定的遥控器做同样的事情.然而,四字拉动",git pull
,将该分支提取到临时位置,FETCH_HEAD
,不更新您的跟踪分支,然后合并 FETCH_HEAD
进入你当前的分支.
What's happening here is that git pull
with no arguments does a git fetch
on the "default remote" (typically origin
), then git merge
s 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.
所以,通过使用 git pull origin blob
,你是在说我想在远程 origin上合并最新版本的
blob
分支code> 进入我当前的分支,而不更新我的任何跟踪分支或获取任何其他数据".
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".
如果你的分支合并配置是正确的(他们可能是,因为 git clone
和 git checkout
尝试自动设置它们),你可以只 git pull
或 git pull origin
它将获取所有内容,然后合并与您当前签出的分支相对应的分支.如果您想要更明确的控制,请使用 git fetch
,然后使用 git merge
.git pull <远程><branch>
很少是你想要的.
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.
(git fetch
在您的示例中修复问题的原因是,在拉取之后,您的本地分支已更新为最新的远程提交,但跟踪分支尚未更新.git fetch
使用这些提交更新跟踪分支,此时差异再次开始有意义.)
(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.)
这篇关于Git Pull 不做 Git Fetch的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!