git 为什么当我需要 *pull* origin master 时会说“你的分支在 857 次提交之前领先于 origin/master”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11032661/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 13:55:07  来源:igfitidea点击:

why does it say "Your branch is ahead of origin/master by 857 commits" when I need to *pull* origin master

git

提问by Anentropic

Firstly, I'm aware of a number of similarly worded questions, eg:

首先,我知道一些措辞相似的问题,例如:

None of them (AFAICT) has an answer that matches my version of this question.

他们(AFAICT)都没有一个与我对这个问题的版本相匹配的答案。

My situation is:

我的情况是:

$ git status
# On branch stable
nothing to commit (working directory clean)
$ git checkout master
Switched to branch 'master'
Your branch is ahead of 'origin/master' by 857 commits.

In the existing questions the accepted and upvoted answers mostly concur that it means literally what it says... I'm ahead and I need to pushmy new commits to origin/master.

在现有问题中,接受和赞成的答案大多同意它的字面意思是它所说的......我领先,我需要我的新提交送到 origin/master。

I know that actually the opposite situation is true, that my local masterbranch is behind the remote origin/masterand actually I need to git pull origin masterbefore doing some work on it locally. (or possibly just git fetch origin?)

我知道实际上相反的情况是正确的,我的本地master分支在远程后面origin/master,实际上我需要git pull origin master在本地做一些工作之前。(或者可能只是git fetch origin?)

My question is... is there some reason for the message to be worded Your branch is ahead of 'origin/master' by 857 commits.such that it literally makes sense?

我的问题是......是否有某种理由使该消息的措辞Your branch is ahead of 'origin/master' by 857 commits.使其字面上有意义?

Because the way I understand it at the moment the meaning is the opposite of what the message says ('my branch' is behindorigin/master).

因为我目前理解它的方式与消息所说的相反(“我的分支”origin/master后面)。

Or does it really mean: "The HEAD of the remote master branch is ahead of your local origin/master tracking branch" ?

或者它真的意味着:“远程 master 分支的 HEAD 领先于您的本地 origin/master 跟踪分支”?

updateFWIW I am working in a team of half a dozen other developers. We all pull, commit and push etc many times a day without problem. I don't have a bug here... I'm just trying to understand why Git words its message this way - whether the wording itself is badly chosen, or if there's some underlying concept of Git that causes them to word it this way and which I'm not understanding properly.

更新FWIW 我在一个由六个其他开发人员组成的团队中工作。我们每天都会多次拉,提交和推送等,没有问题。我这里没有错误……我只是想了解为什么 Git 用这种方式来表达它的信息——是否措辞本身选择不当,或者是否有一些 Git 的潜在概念导致他们以这种方式表达它我没有正确理解。

more info
here is what I guess may be the relevant part of output from git config -l


这里的更多信息是我猜可能是输出的相关部分git config -l

remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*
remote.origin.url=https://code.google.com/a/google.com/p/xxxxx/
branch.master.remote=origin
branch.master.merge=refs/heads/master
branch.master.mergeoptions=--no-ff

回答by Christopher

You are over thinking this. The message isn't saying the remote is behind. It's saying your local repository's recorded commit for 'origin/master' is. At no point in generating that message did git communicate with a remote. It simply looked into the .git directory, and returned the contents of .git/refs/remotes/origin/master. Try it yourself. Both of these commands should return the same thing from the top-level of the repository:

你想多了。该消息并不是说遥控器在后面。它是说您的本地存储库记录的“origin/master”提交是。在生成该消息时,git 从未与远程通信。它只是查看 .git 目录,并返回 .git 目录的内容.git/refs/remotes/origin/master。自己试试吧。这两个命令都应该从存储库的顶层返回相同的内容:

cat .git/refs/remotes/origin/master
git rev-parse origin/master

The second command is simply a plumbing command for finding the 'origin/master' pointer. You could replace 'origin/master' with any branch to get the most recent commit on that branch.

第二个命令只是一个用于查找“原点/主”指针的管道命令。您可以用任何分支替换 'origin/master' 以获得该分支上的最新提交。

That message is telling you is that your local 'master' is ahead of the commit returned by 'git rev-parse origin/master' by 857 commits. How did this situation arise? I can't say exactly, but I'd put considerable money on you accidentally merging a different branch into 'master'. Every time I've seen this problem, it's a bad merge from user error.

该消息告诉您,您的本地 'master' 比 'git rev-parse origin/master' 返回的提交早了 857 次提交。这种情况是如何产生的?我不能确切地说,但是我会在您不小心将不同的分支合并到“master”上时投入大量资金。每次我看到这个问题时,都是用户错误导致的错误合并。

First, issue git fetch originto make sure that 'origin/master' pointer is up-to-date. Then, study your git log. Look for something recent you don't expect. Download a program like tigor use gitkto get a visual graph of your commit history. It's a good bet you accidentally issued git pull stablewhile checked out on 'master'. Or something similar.

首先,发出git fetch origin以确保“origin/master”指针是最新的。然后,研究你的git log. 寻找最近出乎意料的东西。下载类似 tig程序,或使用它gitk来获取提交历史的可视化图表。这是一个很好的赌注,你git pull stable在'master'上签出时不小心发出了。或者类似的东西。

回答by andy magoon

git rebase -p

git rebase -p

It will say First, rewinding head to replay your work on top of it...and you'll be all set, because there's no work to replay.

它会说First, rewinding head to replay your work on top of it...,你一切都准备好了,因为没有工作可以重播。

回答by Ashys

I had this question too. I searched a bit and found out that, we occasionally run 'git pull upstream master' to fetch the latest changes from upstream master to our local master which is a forked branch. However these updated changes are not pushed to our remote master yet. Hence the message says 'our local is x commits ahead of remote master'. Before proceeding with new code or modification in forked branch environment, it's better to run the following commands

我也有这个疑问。我搜索了一下,发现我们偶尔会运行 'git pull upstream master' 来获取从上游 master 到我们本地 master 的最新更改,这是一个分叉分支。但是,这些更新的更改尚未推送到我们的远程主服务器。因此,消息显示“我们的本地是 x 在远程主机之前提交”。在fork分支环境中进行新代码或修改之前,最好运行以下命令

git checkout master_branch;
git pull upstream master;
git push

git checkout master_branch;
git pull 上游大师;

回答by kdo53

I had the same issue. Like the chosen solutionpoints out, I think the problem was the origin/master pointer being out-of-date. I mostly only do git pull origin masterand not fetch.

我遇到过同样的问题。就像所选的解决方案指出的那样,我认为问题在于原点/主指针已过时。我大多只做git pull origin master而不做fetch

Since I know I didn't have local changes, I hard reset to master and then fetched to update the pointer, and finally pulled to catch up on the commits on the remote master branch. Like this:

由于我知道我没有本地更改,因此我硬重置为 master,然后提取更新指针,最后拉动以赶上远程 master 分支上的提交。像这样:

git reset --hard origin/master
git fetch origin
git pull origin master

Hope this helps someone in the future too.

希望这对未来的人也有帮助。

Edit: also usefulOn thisother question, I found a great solutionwith useful commands. These worked for me when the above didn't!

编辑:也有用这个其他的问题,我发现了一个伟大的解决方案提供有用的命令。当以上没有时,这些对我有用!

If you get this message after doing a git pull remote branch, try following it up with a git fetch. (Optionally, run git fetch -pto prune deleted branches from the repo)

Fetch seems to update the local representation of the remote branch, which doesn't necessarily happen when you do a git pull remote branch.

如果您在执行 a 后收到此消息git pull remote branch,请尝试使用 git fetch. (可选地,运行git fetch -p以从 repo 中修剪已删除的分支)

Fetch 似乎更新了远程分支的本地表示,这在您执行git pull remote branch.

回答by blamb

In the end, the Actualsolutionfor the question to correct the problem:

最后,问题的实际解决方案来纠正问题:

It was because it needed a git push, after a merge. This just happened to me too, same error message. Andy magoon was right also, because when I did the push, I see a clean slate now, with no bytes having been pushed. However, rebasing is not usually the best course.

这是因为它在合并后需要 git push。这也发生在我身上,同样的错误信息。Andy magoon 也是对的,因为当我进行推送时,我现在看到了一个干净的石板,没有任何字节被推送。然而,变基通常不是最好的课程。

    $ git status ./
On branch master-blah1
Your branch is ahead of 'origin/master-blah1' by 869 commits.
  (use "git push" to publish your local commits)
nothing to commit, working directory clean


    $ git push
    Counting objects: 7, done.
    Delta compression using up to 48 threads.
    Compressing objects: 100% (7/7), done.
    Writing objects: 100% (7/7), 653 bytes | 0 bytes/s, done.
    Total 7 (delta 4), reused 0 (delta 0)

$ git status ./
On branch master-blah1
Your branch is up-to-date with 'origin/master-blah1'.
nothing to commit, working directory clean