当上游存在更改时,为什么 git status 显示分支是最新的?

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

Why does git status show branch is up-to-date when changes exist upstream?

git

提问by GregB

Changes exist upstream in a tracked branch, but when I type git statusit indicates that my local branch is up-to-date. Is this new behavior, did I change a config setting, or is something wrong?

更改存在于跟踪分支的上游,但是当我键入时,git status它表明我的本地分支是最新的。这是新行为,我是否更改了配置设置,或者有什么问题?

Thanks for the help.

谢谢您的帮助。

ubuntu@host:/my/repo# git status
On branch master
Your branch is up-to-date with 'origin/master'.

nothing to commit, working directory clean


ubuntu@host:/my/repo# git pull
remote: Counting objects: 11, done.
remote: Compressing objects: 100% (11/11), done.
remote: Total 11 (delta 6), reused 0 (delta 0)
Unpacking objects: 100% (11/11), done.
From bitbucket.org:my/repo
   1234567..abcdefg  master     -> origin/master
Updating 1234567..abcdefg
Fast-forward
 file1        |  1 -
 file2        | 43 +++++++++++++++++++++++++++++++++++++++++++
 file3        | 21 ++++++++++++---------
 file4        | 21 ++++++++++++---------
 4 files changed, 67 insertions(+), 19 deletions(-)
 create mode 100644 file5

回答by Jonathan Wakely

What the status is telling you is that you're behind the ref called origin/masterwhich is a local ref in your local repo. In this case that ref happens to track a branch in some remote, called origin, but the status is not telling you anything about the branch on the remote. It's telling you about the ref, which is just a commit ID stored on your local filesystem (in this case, it's typically in a file called .git/refs/remotes/origin/masterin your local repo).

状态告诉您的是,您在被调用的 ref 后面,origin/master它是您本地 repo 中的本地 ref。在这种情况下, ref 恰好跟踪某个名为 的远程分支中的分支origin,但状态并未告诉您有关远程分支的任何信息。它告诉您关于 ref 的信息,它只是存储在本地文件系统中的提交 ID(在这种情况下,它通常位于.git/refs/remotes/origin/master本地存储库中调用的文件中)。

git pulldoes two operations; first it does a git fetchto get up to date with the commits in the remote repo (which updates the origin/masterref in your local repo), then it does a git mergeto merge those commits into the current branch.

git pull做两个操作;首先它会git fetch更新远程仓库中的提交(更新origin/master本地仓库中的引用),然后它会git merge合并这些提交到当前分支。

Until you do the fetchstep (either on its own or via git pull) your local repo has no way to know that there are additional commits upstream, and git statusonly looks at your local origin/masterref.

在您执行该fetch步骤(单独或通过git pull)之前,您的本地存储库无法知道上游还有其他提交,而git status只能查看您的本地origin/master参考。

When git statussays up-to-date, it means "up-to-date with the branch that the current branch tracks", which in this case means "up-to-date with the local ref called origin/master". That only equates to "up-to-date with the upstream status that was retrieved last time we did a fetch" which is not the same as "up-to-date with the latest live status of the upstream".

git status说最新时,它的意思是“与当前分支跟踪的分支保持同步”,在这种情况下,这意味着“与调用的本地引用保持同步origin/master”。这仅等同于“与我们上次执行时检索到的上游状态保持同步”,这与“与上游fetch的最新实时状态保持同步”不同。

Why does it work this way? Well the fetchstep is a potentially slow and expensive network operation. The design of Git (and other distributedversion control systems) is to avoid network operations when unnecessary, and is a completely different model to the typical client-server system many people are used to (although as pointed out in the comments below, Git's concept of a "remote tracking branch" that causes confusion here is not shared by all DVCSs). It's entirely possible to use Git offline, with no connection to a centralized server, and the output of git statusreflects this.

为什么它以这种方式工作?那么这fetch一步是一个潜在的缓慢和昂贵的网络操作。Git(和其他分布式版本控制系统)的设计是为了避免不必要的网络操作,与许多人习惯的典型客户端 - 服务器系统完全不同(尽管在下面的评论中指出,Git的概念在此处引起混淆的“远程跟踪分支”并非所有 DVCS 共享)。完全可以离线使用 Git,无需连接到中央服务器,输出git status反映了这一点。

Creating and switching branches (and checking their status) in Git is supposed to be lightweight, not something that performs a slow network operation to a centralized system. The assumption when designing Git, and the git statusoutput, was that users understand this (too many Git features only make sense if you already know how Git works). With the adoption of Git by lots and lots of users who are not familiar with DVCS this assumption is not always valid.

在 Git 中创建和切换分支(并检查它们的状态)应该是轻量级的,而不是对集中式系统执行缓慢网络操作的东西。设计 Git 和git status输出时的假设是用户理解这一点(只有当您已经知道 Git 的工作原理时,太多的 Git 功能才有意义)。随着大量不熟悉 DVCS 的用户采用 Git,这个假设并不总是有效的。

回答by Ryan

This is because your local repo hasn't checked in with the upstream remotes. To have this work as you're expecting it to, use git fetchthen run a git statusagain.

这是因为您的本地存储库尚未与上游遥控器签入。要像您期望的那样工作,请使用git fetch然后git status再次运行。

回答by Skycube

While these are all viable answers, I decided to give my way of checking if local repo is in line with the remote, whithout fetching or pulling. In order to see where my branches are I use simply:

虽然这些都是可行的答案,但我决定给出我的方法来检查本地 repo 是否与远程一致,无需获取或拉动。为了查看我的分支在哪里,我简单地使用:

git remote show origin

What it does is return all the current tracked branches and most importantly - the info whether they are up to date, ahead or behind the remote origin ones. After the above command, this is an example of what is returned:

它所做的是返回所有当前跟踪的分支,最重要的是 - 无论它们是最新的、领先于还是落后于远程源分支的信息。在上述命令之后,这是返回内容的示例:

  * remote origin
  Fetch URL: https://github.com/xxxx/xxxx.git
  Push  URL: https://github.com/xxxx/xxxx.git
  HEAD branch: master
  Remote branches:
    master      tracked
    no-payments tracked
  Local branches configured for 'git pull':
    master      merges with remote master
    no-payments merges with remote no-payments
  Local refs configured for 'git push':
    master      pushes to master      (local out of date)
    no-payments pushes to no-payments (local out of date)

Hope this helps someone.

希望这可以帮助某人。

回答by Dejan Baric llevo3

try with git add .before git commit

git add .之前尝试git commit

回答by krustyengineer

Trivial answer yet accurate in some cases, such as the one that brought me here. I was working in a repo which was new for me and I added a file which was not seen as new by the status.

在某些情况下,简单但准确的答案,例如将我带到这里的那个。我在一个对我来说是新的存储库中工作,我添加了一个文件,该文件不被状态视为新文件。

It ends up that the file matched a pattern in the .gitignore file.

最终该文件与 .gitignore 文件中的模式匹配。

回答by Airton Silveira

in this case use git add and integrate all pending files and then use git commit and then git push

在这种情况下,使用 git add 并集成所有挂起的文件,然后使用 git commit 和 git push

git add - integrate all pedent files

git add - 整合所有 pedent 文件

git commit - save the commit

git commit - 保存提交

git push - save to repository

git push - 保存到仓库

回答by Marek Stanley

"origin/master" refers to the reference poiting to the HEAD commit of branch "origin/master". A reference is a human-friendly alias name to a Git object, typically a commit object. "origin/master" reference only gets updated when you git pushto your remote (http://git-scm.com/book/en/v2/Git-Internals-Git-References#Remotes).

“origin/master”指的是指向分支“origin/master”的HEAD提交的引用。引用是对 Git 对象(通常是提交对象)的人性化别名。“origin/master”参考仅在您git push使用遥控器时更新(http://git-scm.com/book/en/v2/Git-Internals-Git-References#Remotes)。

From within the root of your project, run:

从项目的根目录中,运行:

cat .git/refs/remotes/origin/master

Compare the displayed commit ID with:

将显示的提交 ID 与:

cat .git/refs/heads/master

They should be the same, and that's why Git says masteris up-to-date with origin/master.

它们应该是相同的,这就是为什么 Git 说master是最新的origin/master.

When you run

当你跑

git fetch origin master

That retrieves new Git objects locally under .git/objects folder. And Git updates .git/FETCH_HEAD so that now, it points to the latest commit of the fetched branch.

这会在 .git/objects 文件夹下本地检索新的 Git 对象。并且 Git 更新 .git/FETCH_HEAD 以便现在它指向获取分支的最新提交。

So to see the differences between your current local branch, and the branch fetched from upstream, you can run

因此,要查看当前本地分支与从上游获取的分支之间的差异,您可以运行

git diff HEAD FETCH_HEAD

回答by themefield

Let look into a sample git repo to verify if your branch (master)is up to datewith origin/master.

让我们寻找到一个样品混帐回购协议,以验证是否your branch (master)up to dateorigin/master

Verify that local master is tracking origin/master:

验证本地 master 是否正在跟踪 origin/master:

$ git branch -vv
* master a357df1eb [origin/master] This is a commit message

More info about local master branch:

有关本地主分支的更多信息:

$ git show --summary
commit a357df1eb941beb5cac3601153f063dae7faf5a8 (HEAD -> master, tag: 2.8.0, origin/master, origin/HEAD)
Author: ...
Date:   Tue Dec 11 14:25:52 2018 +0100

    Another commit message

Verify if origin/master is on the same commit:

验证 origin/master 是否在同一个提交上:

$ cat .git/packed-refs | grep origin/master
a357df1eb941beb5cac3601153f063dae7faf5a8 refs/remotes/origin/master

We can see the same hash around, and safe to say the branch is in consistency with the remote one, at least in the current git repo.

我们可以看到相同的散列,并且可以肯定地说该分支与远程分支一致,至少在当前的 git 存储库中是这样。