简体   繁体   English

Git:您的分支领先于 'upstream/master' 2 次提交

[英]Git: Your branch is ahead of 'upstream/master' by 2 commits

I have seen something like: Your branch is ahead of 'origin/master' by 2 commits .我看到过类似的内容: Your branch is ahead of 'origin/master' by 2 commits But this time I am getting upstream/master instead of origin/master .但这次我得到了upstream/master而不是origin/master Not able to get why?不明白为什么?

abc@xyz MINGW64 /d/new (master)
$ git status
On branch master
Your branch is ahead of 'upstream/master' by 2 commits.
  (use "git push" to publish your local commits)

Prior to this, I had updated my master (twice) via git pull upstream master , which surprisingly created a new commit for the update.在此之前,我通过git pull upstream master更新了我的 master(两次),这令人惊讶地为更新创建了一个新的提交。 I think that these two update commits are the 2 commits that the local master is "ahead of the upstream/master".我认为这两个更新提交是本地主节点“领先于上游/主节点”的 2 个提交。 But is there any specific reason/solution to this?但是有什么具体的原因/解决方案吗?

Now, If I merge my master to any branch, these 2 commits also become a part of the branch and of course, occur in my PR when I push it.现在,如果我将我的 master 合并到任何分支,这 2 个提交也会成为分支的一部分,当然,当我推送它时,会发生在我的 PR 中。

I am working on Windows 7.我正在使用 Windows 7。

I have seen something like: Your branch is ahead of ' origin /master' by 2 commits .我看到过类似的内容: Your branch is ahead of ' origin /master' by 2 commits But this time I am getting upstream /master ... why?但是这次我正在upstream /master ......为什么?

You set the upstream of your branch to upstream/master .您将分支的上游设置为upstream/master Every branch can have one upstream (or no upstream, if you like).每个分支都可以有一个上游(如果你愿意,也可以没有上游)。 The git status command will compare reachable commits from the branch and its upstream, counting, and tell you if your branch is ahead, behind, or diverged based on these counts. git status命令将比较来自分支及其上游的可达提交,计数,并根据这些计数告诉您您的分支是领先、落后还是发散。

You only get one upstream (or no upstream) setting per branch.每个分支只能获得一个上游(或没有上游)设置。 Note that the word "upstream" here is not the same as the upstream remote , ie, the fact that some branch has some upstream is not dependent on the fact that you have a remote named upstream .请注意,这个词“上游”这里是一样的upstream远端,即一些分支有一些上游的事实是不依赖于事实,你有一个名为远程upstream (Git is very bad about using words confusingly like this.) (Git 在使用像这样令人困惑的词时非常糟糕。)

But is there any specific reason [for being two ahead]?但是[领先两个]有什么具体原因吗?

git pull means Run two Git commands: First, run git fetch . git pull表示运行两个 Git 命令:首先,运行git fetch Then, run a second Git command of my choice, git merge or git rebase , using whatever you got when you ran the first Git command.然后,运行我选择的第二个 Git 命令, git mergegit rebase ,使用你在运行第一个 Git 命令时得到的任何东西。

In this case, you ran git pull upstream master , so this ran:在这种情况下,您运行了git pull upstream master ,因此运行:

git fetch upstream

followed by (probably, unless you changed your settings)其次是(可能,除非您更改了设置)

git merge <something>

The <something> here is a little tricky to explain, 1 but git merge can, and in this case probably did, create a new commit.这里的 <something> 有点难以解释, 1但是git merge可以,在这种情况下可能会创建一个新的提交。

To know exactly what happened, we would have to know:要确切知道发生了什么,我们必须知道:

  • what was in each repository before the git fetch , which tells us git fetch之前每个存储库中的内容,它告诉我们
  • what the git fetch did, which lets us determine git fetch做了什么,这让我们可以确定
  • what the git merge did. git merge做了什么。

I generally recommend that those new to Git avoid git pull and instead (1) learn what each of these commands ( git fetch , git merge , and git rebase ) does, then (2) use the commands individually and separately.我通常建议那些刚接触 Git 的人避免使用git pull而是 (1) 了解这些命令中的每一个( git fetchgit mergegit rebase )的作用,然后 (2) 单独和单独使用这些命令。 This can still be confusing and a bit difficult but you have a much better chance of understanding what is going on, if you do it this way.这仍然可能令人困惑并且有点困难,但是如果您这样做,您将有更好的机会了解正在发生的事情。

Note that when you do this, you have the opportunity to insert some third Git command between the two.请注意,当您这样做时,您有机会在两者之间插入一些第三个Git 命令。 For example, when you know how to interpret git log output, you can say: I ran fetch, now I'd like to run git log --all --decorate --oneline --graph to see what git fetch did.例如,当你知道如何解释git log输出时,你可以说:我运行了 fetch,现在我想运行git log --all --decorate --oneline --graph来看看git fetch做了什么。

(Once you know what each command does and can predict everything, then it's safe, or at least safe-ish, to use the all-in-one convenience "fetch and then merge-or-rebase" git pull . I still mostly don't: I like to run those extra git log commands, or some of them, based on what git fetch says.) (一旦您知道每个命令的作用并且可以预测所有内容,那么使用多合一的便利“获取然后合并或变基” git pull是安全的,或者至少是安全的。我仍然大多不't:我喜欢根据git fetch所说的运行那些额外的git log命令,或其中的一些命令。)

If you want to get rid of these merge commits, you can do this with git reset or with most varieties of git rebase .如果你想摆脱这些合并提交,你可以使用git reset或大多数种类的git rebase来做到这一点。 But maybe you should just keep these merge commits—that might be best.但也许你应该保留这些合并提交——这可能是最好的。 What to use, and when, is a matter of situation and also personal taste in some cases—there's no blanket always-right action here.在某些情况下,使用什么以及何时使用取决于情况和个人品味——这里没有一揽子永远正确的行动。 Git provides a lot of individual tools with which you can build your own solutions, but for the most part, isn't a solution itself. Git 提供了许多单独的工具,您可以使用这些工具构建自己的解决方案,但在大多数情况下,它本身并不是一个解决方案。


1 git fetch leaves, in a file in your Git repository named FETCH_HEAD , some trace information. 1 git fetch在 Git 存储库中名为FETCH_HEAD的文件中FETCH_HEAD一些跟踪信息。 The pull command fishes this information back out and builds the git merge command from that. pull 命令将这些信息取出并从中构建git merge命令。 Given the arguments upstream and master , the effect is much the same as if you had run git merge upstream/master , but there are some minor differences.给定upstreammaster参数,效果与运行git merge upstream/master大致相同,但有一些细微差别。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM