本文介绍了'git pull origin mybranch' 使本地 mybranch N 在原点之前提交.为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚观察到关于 git pull 的一些奇怪的东西,我不明白.

I just observed something odd about git pull, which I don't understand.

周五,我在当地一家分公司工作.我们称之为 mybranch.在离开办公室之前,我将它推送到 origin(这是我的 github 存储库):git push origin mybranch.

On Friday, I worked on a local branch. let's call it mybranch. Before leaving the office I pushed it to origin (which is my github repo): git push origin mybranch.

昨天在家里,我mybranch 到我的笔记本电脑,做了一些更多的编码,然后将我的更改推送回 github(来源).

Yesterday at home, I pulled mybranch to my laptop, did some more coding, and then pushed my changes back to github (origin).

现在我又开始工作了,并尝试将昨天的更改拉到我的工作机器上(周末我没有更改我工作地点的本地存储库中的任何内容):

Now I'm at work again, and tried to pull the changes from yesterday to my work machine (I didn't change anything in my work place's local repo over the weekend):

git pull origin mybranch

这导致了快进合并,这很好.然后我做了一个 git status,它说:

that caused a fast forward merge, which is fine. I then did a git status, and it said:

# On branch mybranch
# Your branch is ahead of 'origin/mybranch' by 6 commits.
#
nothing to commit (working directory clean)

嗯?当我在周末甚至没有触及它时,它怎么能提前 6 次提交,并且只是从原点拉出?所以我运行了一个 git diff origin/mybranch 并且差异正是我刚刚从远程拉取的 6 个更改.

Huh? How can it be 6 commits ahead when I didn't even touch it over the weekend, AND just pulled from origin? So I ran a git diff origin/mybranch and the diffs were exactly the 6 changes I just pulled from remote.

我只能通过运行 git fetch origin 来修复"这个:

I could only "fix" this by running git fetch origin:

From [email protected]:me/project
af8be00..88b0738  mybranch -> origin/mybranch

显然,我的本地存储库缺少一些参考对象,但这怎么可能?我的意思是,pull 已经做了一个 fetch,除了那个分支我没有做任何事情,所以 git fetch origingit fetch origin mybranch 应该有相同的结果?

Apparently, my local repo was missing some reference objects, but how can that be? I mean, a pull does a fetch already, and I didn't work on anything except that branch, so a git fetch origin and git fetch origin mybranch should have the same result?

我应该总是使用 git pull origin 而不是 git pull origin branchname 吗?

Should I always use git pull origin instead of git pull origin branchname?

我很困惑.

推荐答案

git pull 使用适当的参数调用 git fetch 在合并显式获取的头部之前(或者如果没有配置为合并的远程分支)到当前分支.

git pull calls git fetch with the appropriate parameters before merging the explicitly fetched heads (or if none the remote branch configured for merge) into the current branch.

语法:git fetch <ref> 其中 <ref> 只是一个没有冒号的分支名称是一次性"获取,它不会对所有跟踪的分支进行标准获取指定的远程,而是仅将命名分支提取到 FETCH_HEAD 中.

The syntax: git fetch <repository> <ref> where <ref> is just a branch name with no colon is a 'one shot' fetch that doesn't do a standard fetch of all the tracked branches of the specified remote but instead fetches just the named branch into FETCH_HEAD.

更新:对于 1.8.4 之后的 Git 版本,如果有一个远程跟踪分支跟踪您要求获取的引用,那么跟踪分支现在将通过 fetch.进行此更改是为了避免之前的行为造成的混淆.

Update: for Git versions since 1.8.4, if there is a remote tracking branch which tracks the ref that you asked to fetch then the tracking branch will now be updated by fetch. This change has been made specifically to avoid the confusion that the previous behaviour caused.

当你执行 git pull ;<ref>, FETCH_HEAD 如上更新,然后合并到您检出的 HEAD 中,但远程存储库的标准跟踪分支都不会更新(Git 看起来你领先于远程分支,而实际上你是最新的.

When you perform git pull <repository> <ref>, FETCH_HEAD is updated as above, then merged into your checked out HEAD but none of the standard tracking branches for the remote repository will be updated (Git <1.8.4). This means that locally it looks like you are ahead of of the remote branch, whereas in fact you are up to date with it.

我个人总是先执行 git fetch 然后是 git merge / 因为我在合并之前会看到任何关于强制更新的警告,并且我可以预览我正在合并的内容.如果我使用 git pull 比我多一点,我会做一个没有参数的普通 git pull时间,依靠 branch..remotebranch..merge 来做正确的事".

Personally I always do git fetch followed by git merge <remote>/<branch> because I get to see any warnings about forced updates before I merge, and I can preview what I'm merging in. If I used git pull a bit more than I do, I would do a plain git pull with no parameters most of the time, relying on branch.<branch>.remote and branch.<branch>.merge to 'do the right thing'.

这篇关于'git pull origin mybranch' 使本地 mybranch N 在原点之前提交.为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 17:20