为什么 git log 在 git fetch 之后没有显示任何新内容?

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

Why does git log not show anything new after git fetch?

gitgit-pullgit-fetch

提问by Adil

I am learning about working with Git remotes by reading the relevant section of the Pro Git Book.

我正在通过阅读Pro Git Book相关部分来学习使用 Git 遥控器。

If you clone a repository, the command automatically adds that remote repository under the name "origin". So, git fetch originfetches any new work that has been pushed to that server since you cloned (or last fetched from) it.

如果您克隆存储库,该命令会自动添加名为“origin”的远程存储库。因此,git fetch origin获取自您克隆(或上次从中获取)以来已推送到该服务器的任何新工作。

It's important to note that the git fetchcommand only fetchesthe data to your local repository; it doesn't automatically merge it with any of your work or modify what you're currently working on. You have to merge it manually into your work when you're ready.

需要注意的是,该git fetch命令仅将数据提取到您的本地存储库;它不会自动将其与您的任何工作合并或修改您当前正在处理的内容。准备好后,您必须手动将其合并到您的工作中。

Here is what I tried. I cloned a repository and edited a file. In the original repository, someone updated the same file and pushed. Then,

这是我尝试过的。我克隆了一个存储库并编辑了一个文件。在原始存储库中,有人更新了相同的文件并进行了推送。然后,

  1. I ran git fetch. It showed some update progress message. However, git logdid not show that update. Did I misunderstand what git fetchdoes? Am I missing something?

  2. I ran git pull, and I got

  1. 我跑了git fetch。它显示了一些更新进度消息。但是,git log并没有显示更新。我误解了什么git fetch?我错过了什么吗?

  2. 我跑了git pull,我得到了

error: Your local changes to 'hello_world.c' would be overwritten by merge. Aborting. Please, commit your changes or stash them before you can merge.

错误:您对“hello_world.c”的本地更改将被合并覆盖。中止。请在合并之前提交您的更改或隐藏它们。

Here, I believe it's also merging and to avoid accidental data loss, it aborts.

在这里,我相信它也在合并,为了避免意外的数据丢失,它会中止。

Edit:Thanks for the answers. Actually before looking at the answers, I was trying myself and realized the same with the following commands / outputs:

编辑:感谢您的回答。实际上,在查看答案之前,我正在尝试自己并使用以下命令/输出实现相同的效果:

$ git ls-remote origin
d0006a6bfa95e0e90aa820a0e50d31a548625652    HEAD
d0006a6bfa95e0e90aa820a0e50d31a548625652    refs/heads/master
$ git ls-remote .
14375458b8a6b84f82d9fa4d2ded0bb8c9e87431    HEAD
14375458b8a6b84f82d9fa4d2ded0bb8c9e87431    refs/heads/master
d0006a6bfa95e0e90aa820a0e50d31a548625652    refs/remotes/origin/HEAD
d0006a6bfa95e0e90aa820a0e50d31a548625652    refs/remotes/origin/master

Also with following commands:

还可以使用以下命令:

$git log origin --oneline
$git log --oneline

Thank you for bearing with my stupid questions ;-)

谢谢你忍受我愚蠢的问题;-)

采纳答案by Adil

By default, git logshows the log of the current branch. Since fetching without merging doesn't change anything in your current (local) branch, the git logoutput doesn't change.

默认git log显示当前分支的日志。由于在不合并的情况下获取不会更改当前(本地)分支中的任何内容,因此git log输出不会更改。

git logcan take the --alloption to show the history of all branches, local and remote. Alternatively, you can explicitly list the remote branch as in git log origin/master.

git log可以--all选择显示本地和远程所有分支的历史记录。或者,您可以显式列出远程分支,如git log origin/master

回答by jub0bs

You wrote

你写了

However, git log did not show that update. Did I misunderstand what git fetch does? Am I missing something?

但是, git log 没有显示该更新。我误解了 git fetch 的作用吗?我错过了什么吗?

You seem to be confused about what both git logand git fetchdo.

你似乎对两者git loggit fetch做什么感到困惑。

Under the assumption that a branch called masteris currently checked out—you may also be in detached-HEAD state, but let's keep things simple, for the sake of this explanation—the command git log, without any other arguments, is equivalent to git log master. This command tells Git

假设master当前调用的分支已被检出——您也可能处于 detached-HEAD 状态,但为了便于说明,让我们保持简单——命令git log,不带任何其他参数,等效于git log master。这个命令告诉 Git

Show me all the commits that are in the ancestry of the local masterbranch.

向我展示本地master分支的祖先中的所有提交。

However, you need to understand that git fetch origindoesn't affect/update your local branches, such as master. Instead, new commits from origin(or any other remote you fetch from) are put in remote-tracking branches.

但是,您需要了解这git fetch origin不会影响/更新您的本地分支,例如master. 相反,来自origin(或您从中获取的任何其他远程)的新提交被放置在远程跟踪分支中

Remote-tracking branches are speciallocal branches whose sole purpose is to reflect the state of branches that live in a remote repository at the time of your last communication with the server. In particular, you can't commit to branches of this type.

远程跟踪分支是特殊的本地分支,其唯一目的是反映上次与服务器通信时位于远程存储库中的分支的状态。特别是,您不能提交到这种类型的分支。

What you probably want is to run

你可能想要的是运行

git fetch origin

to fetch all new commits from originand puts them in remote-tracking branches (including origin/master, here), and then

从中获取所有新提交origin并将它们放入远程跟踪分支(包括origin/master, 此处),然后

git log master..origin/master

to show a log of all the "new" commits that were added on the remote masterbranch, i.e. the branch that lives on the remote server you know as origin, which your remote-tracking branch origin/masterkeeps track of.

显示在远程master分支上添加的所有“新”提交的日志,即位于您所知的远程服务器上的分支,origin您的远程跟踪分支origin/master会跟踪它。

回答by Melebius

After git fetch, your local repo knows the changes from the remote repo but hasn't applied them to your local branches yet.

之后git fetch,您的本地存储库知道来自远程存储库的更改,但尚未将它们应用到您的本地分支。

git logwithout additional parameters shows the log of the current branch – and you haven't merged the remote changes into that branch yet.

git log没有附加参数显示当前分支的日志——并且您还没有将远程更改合并到该分支中。

git pulldoes git fetchand git merge FETCH_HEAD.

git pullgit fetchgit merge FETCH_HEAD

Your error message means that there are uncommitted changes in your local repo. You can commit or stash them (as the message states) and try merge(or pull) again.

您的错误消息意味着您的本地存储库中有未提交的更改。您可以提交或隐藏它们(如消息所述)并再次尝试merge(或pull)。

回答by Mohammad AbuShady

Your local branches never get updated from a fetch, only the tracking branches do, git pullon the other hand does,

您的本地分支永远不会从获取中更新,只有跟踪分支会更新,git pull另一方面,

git pull= git fetch+ git merge

git pull= git fetch+git merge

You can't see any updates in git logsimply beause you are running it on the wrong branch, to see your changes from just a git fetchthen you need to git logon the right branch, which in your case would be

您看不到任何更新git log只是因为您在错误的分支上运行它,要查看您的更改,git fetch然后您需要git log在正确的分支上进行更改,在您的情况下是

git log origin/master