git '您的分支在显式推送时领先于 'origin/master' 1 次提交'

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9050978/
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 12:51:32  来源:igfitidea点击:

'Your branch is ahead of 'origin/master' by 1 commit' on explicit push

git

提问by Basel Shishani

I'm new to git, and I can't make sense of the following behavior:

我是 git 新手,无法理解以下行为:

I have a bitbucket repo that I clone locally, then I modify and commit locally, then I push to the remote repo. If I do an implicit push, or 'push origin', there's no issue. If I push using explicit remote url, I get the status message 'Your branch is ahead of 'origin/master' by 1 commit'.

我有一个在本地克隆的 bitbucket 存储库,然后在本地修改和提交,然后推送到远程存储库。如果我进行隐式推送或“推送原点”,则没有问题。如果我使用显式远程 url 推送,我会收到状态消息“您的分支在 1 次提交之前领先于‘origin/master’”。

It seems that git does not recognize the explicit address as being the same as origin, so what exactly is going on. If I do another implicit push, it does nothing, but it does clear the said message.

似乎 git 没有将显式地址识别为与原点相同,那么到底发生了什么。如果我再进行一次隐式推送,它什么也不做,但它确实清除了所述消息。

Here's a sample session:

这是一个示例会话:

baz@bhost:/more/coding/git-tests/ask1$ git --version
git version 1.7.2.5

baz@bhost:/more/coding/git-tests$ git clone https://[email protected]/shishani/dirasi.git ask1
Cloning into ask1...
Password: 
remote: Counting objects: 24054, done.
remote: Compressing objects: 100% (6300/6300), done.
remote: Total 24054 (delta 17124), reused 24024 (delta 17106)
Receiving objects: 100% (24054/24054), 11.83 MiB | 251 KiB/s, done.
Resolving deltas: 100% (17124/17124), done.

baz@bhost:/more/coding/git-tests$ cd ask1

baz@bhost:/more/coding/git-tests/ask1$ jed setup.py

baz@bhost:/more/coding/git-tests/ask1$ git commit -a
[master a053f28]    modified:   setup.py
 1 files changed, 1 insertions(+), 0 deletions(-)

baz@bhost:/more/coding/git-tests/ask1$ git status
# On branch master
# Your branch is ahead of 'origin/master' by 1 commit.
#
nothing to commit (working directory clean)

baz@bhost:/more/coding/git-tests/ask1$ git remote show origin
Password: 
* remote origin
  Fetch URL: https://[email protected]/shishani/dirasi.git
  Push  URL: https://[email protected]/shishani/dirasi.git
  HEAD branch: master
  Remote branch:
    master tracked
  Local branch configured for 'git pull':
    master merges with remote master
  Local ref configured for 'git push':
    master pushes to master (fast-forwardable)

baz@bhost:/more/coding/git-tests/ask1$ git push
Password: 
Counting objects: 5, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 314 bytes, done.
Total 3 (delta 2), reused 0 (delta 0)
remote: bb/acl: shishani is allowed. accepted payload.
To https://[email protected]/shishani/dirasi.git
   5e9ffd1..a053f28  master -> master

baz@bhost:/more/coding/git-tests/ask1$ git status
# On branch master
nothing to commit (working directory clean)

baz@bhost:/more/coding/git-tests/ask1$ jed setup.py

baz@bhost:/more/coding/git-tests/ask1$ git commit -a
[master 6d0e236]    modified:   setup.py
 1 files changed, 1 insertions(+), 0 deletions(-)

baz@bhost:/more/coding/git-tests/ask1$ git push https://[email protected]/shishani/dirasi.git master
Password: 
Counting objects: 5, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 298 bytes, done.
Total 3 (delta 2), reused 0 (delta 0)
remote: bb/acl: shishani is allowed. accepted payload.
To https://[email protected]/shishani/dirasi.git
   a053f28..6d0e236  master -> master

baz@bhost:/more/coding/git-tests/ask1$ git status
# On branch master
**# Your branch is ahead of 'origin/master' by 1 commit.** <-- this does not reflect current state
#
nothing to commit (working directory clean)

baz@bhost:/more/coding/git-tests/ask1$ git push
Password: 
Everything up-to-date

baz@bhost:/more/coding/git-tests/ask1$ git status
# On branch master
nothing to commit (working directory clean)

采纳答案by Borealid

I believe you need to pull the change back in before gitwill realize that the remote end has been updated. You can do this via a quick git pull(or, as you show in your example, a git pushto the tracked origin).

我相信您需要在git意识到远程端已更新之前将更改拉回来。您可以通过快速git pull(或者,如您的示例中所示,git push指向跟踪的原点)来执行此操作。

You could also manually change the commit to which refs/remotes/originpoints.

您还可以手动更改提交到哪些refs/remotes/origin点。

It's not fair to expect the content tracker to recognize arbitrary remote synonyms for repositories; imagine if you had five different URLs which all were the same server-side repository. When you push to repo A, does that mean that your changes have been committed to repo B (the origin of your branch)? git has no way to know. Instead, it only updates the remote-head reference in two cases: a pull, and a push to the default destination.

期望内容跟踪器识别存储库的任意远程同义词是不公平的;想象一下,如果您有五个不同的 URL,它们都是同一个服务器端存储库。当您推送到 repo A 时,是否意味着您的更改已提交到 repo B(您的分支的来源)?git 无法知道。相反,它只在两种情况下更新远程头引用:拉动和推送到默认目的地。

回答by Sailesh

When the status says Your branch is ahead of 'origin/master' by 1 commit, it actually does mean origin/master. That is, there is pointer named origin/masterin your repo pointing to the commit which is the HEADof that remote branch, and your masteris ahead of this commit.

当状态说的时候Your branch is ahead of 'origin/master' by 1 commit,它实际上确实意味着origin/master。也就是说,origin/master您的 repo 中有一个指向提交的指针,该提交是HEADthat的提交remote branch,并且您master在此提交之前。

For all the remotes that your repo recognizes, it creates a <remote>/<branchname>pointer in the repo, if you pull/push/fetch. Its just a pointer to the commit, and if you try to checkoutthat branch, you'd only be in a detached state.

对于您的存储库识别的所有遥控器<remote>/<branchname>,如果您拉/推/获取,它会在存储库中创建一个指针。它只是一个指向提交的指针,如果你尝试到checkout那个分支,你只会处于分离状态。

Apparently, this pointer gets updated when you do git pushor git push origin master, but explicitly pushing to a url would not update that pointer, and hence the status.

显然,当您执行git pushor时,此指针会更新git push origin master,但显式推送到 url 不会更新该指针,因此不会更新状态。

If you just do git fetchor git pullafter pushing to the url, the status message will also disappear in that case.

如果您只是这样做git fetchgit pull在推送到 url 之后,状态消息也会在这种情况下消失。

NOTE: If you have multiple remotes, and you set your branch to track some other remote branch, like say upstream master, your status message will in this case be, for example, Your branch is ahead of 'upstream/master' by 1 commit. So its only the tracked remote branch that the comparison is done. See git configto see what remote branch your current branch is tracking.

注意:如果您有多个遥控器,并且您将分支设置为跟踪某个其他远程分支,例如 say upstream master,则在这种情况下,您的状态消息将是,例如,Your branch is ahead of 'upstream/master' by 1 commit。所以它只是比较完成的跟踪远程分支。查看git config您当前的分支正在跟踪哪个远程分支。

回答by manojlds

Yes, you will have to do a git fetchor git fetch originin that case. Git doesn't care where you push to, only the remote (and remote tracking branches) is all that matters when reporting the status of how many commits you are ahead etc.

是的,在这种情况下,您必须执行 agit fetchgit fetch origin。Git 不关心你推送到哪里,只有远程(和远程跟踪分支)在报告你提前提交的状态等时才是最重要的。

回答by VonC

Note that, since git1.8.1rc1(December 2012), Git will include hint messages:

请注意,从git1.8.1rc1(2012 年 12 月)开始,Git将包含提示消息

if (advice_status_hints)
+      strbuf_addf(sb,
+        _("  (use \"git push\" to publish your local commits)\n"));

When "git checkout" checks out a branch, it tells the user how far behind (or ahead) the new branch is relative to the remote tracking branch it builds upon.
The message now also advises how to sync them up by pushing or pulling.

This can be disabled with the advice.statusHintsconfiguration variable.

当“ git checkout”签出一个分支时,它会告诉用户新分支相对于它所建立的远程跟踪分支落后(或领先)多远。
该消息现在还建议如何通过推或拉来同步它们。

这可以通过advice.statusHints配置变量禁用。