git fetch 和 git fetch origin master 之间的区别

2023-12-08

I was 进行获取/合并并想知道这样做是否有什么区别

git fetch

and

git fetch origin master

我没有任何其他分支和起源点到我的remote repository在 GitHub 上。

当我做:

git fetch origin master
remote: Counting objects: 4, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 1), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
From github.com:XXXXXXXXXXXXXXX
 * branch            master     -> FETCH_HEAD

只是:

git fetch
From github.com:XXXXXXXXXXXXXXX
   531d466..aaf6df0  master     -> origin/master

请注意,master 指向不同的事物;在一种情况下FETCH_HEAD在其他情况下,origin/master? 它们有什么不同吗?


这是“TL;DR”版本(它掩盖了lot特殊情况):git fetch always更新FETCH_HEAD,在各种情况下都有不止一条线。它有时更新“远程分支”,它们是全名开头的引用refs/remotes/。其余的主要是关于“有时”,它根据给出的参数数量而变化git fetch,以及 git 版本。


我有机会测试一下。让我们区分三种情况,所有这些都假设正在运行git fetch没有额外的选项,例如-a甚至--all。我们还排除更奇怪的变体git fetch,就像直接使用 URL 一样,或者insteadOf条目或列出的文件.git/remotes or .git/branches。 (我承认我只是猜测,但我认为这些都是前几天留下的[remote "name"]条目进入 git 的配置文件。 2019 年编辑:事实证明这是正确的。)

  1. git fetch,并且没有其他参数。

    Git 确定您当前的分支(以通常的方式,通过阅读HEAD,但你当然可以看到它是什么git branch or git status)。然后它会查找该分支的配置条目,并将其命名为remote。例如,假设您在分支机构dummy and .git/config有(以及其他条目):

    [branch "dummy"]
        remote = remote-X
    

    在这种情况下git fetch相当于git fetch remote-X。之后,这相当于情况 2,即:

  2. git fetch remote(除此之外没有更多的争论)。

    Git 这次不会查看你当前的分支。要使用的遥控器是命令行上给出的遥控器。它does查找给定遥控器的配置部分。假设你正在使用remote-X:在这种情况下,它会查找:

    [remote "remote-X"]
        url = ...
    

    If that section does not exist, or there is no url = entry, you get an error: fatal: 'remote-X' does not appear to be a git repository.1 Otherwise that gives the URL, and git fetch will attempt to connect to there. Assuming it can connect...

    通常还有至少一个配置条目,可能还有更多,内容如下:

        fetch = +refs/heads/*:refs/remotes/remote-X/*
    

    (遥控器的名称在这里是硬编码的)。假设有...

    Next, git fetch询问遥控器它有什么引用(主要是分支和标签,虽然你可以获得所有引用,但大多数人只关心分支和标签)。你可以自己做同样的事情git ls-remote remote-X,它会溢出这样的东西:

    676699a0e0cdfd97521f3524c763222f1c30a094        HEAD
    222c4dd303570d096f0346c3cd1dff6ea2c84f83        refs/heads/branch
    676699a0e0cdfd97521f3524c763222f1c30a094        refs/heads/master
    

    The treatment of the HEAD ref is not entirely consistent (I've seen it behave oddly) but usually here it just gets dropped.2 The remaining branches are renamed and updated according to the fetch = refspec. (If there are multiple fetch = refspecs, they're renamed and updated according to all of them. This is mainly useful for bringing over refs/notes/ or making your own "remote tags" name-space under refs/rtags/, for instance.)

    在这种情况下,fetch 将带来两个分支所需的任何对象branch and master,并更新(本地)“远程分支”名称,refs/remotes/remote-X/branch and refs/remotes/remote-X/master, 如所须。对于每一个更新的内容,fetch打印这样一行:

       22b38d1..676699a  master     -> remote-X/master
    

    If the fetch =线路缺失,你会得到完全不同的东西。输出将显示:

     * branch            HEAD       -> FETCH_HEAD
    

    在这种情况下,就好像(缺失)fetch =线路在那里并且被包含fetch = HEAD.

  3. git fetch remote refspec (the refspec部分实际上是一个或多个参考规范,如下所述)。

    这与情况 2 类似,只是这一次,“refspecs”是在命令行上提供的,而不是从fetch =远程的配置条目。然而,这里的获取行为有很大不同。


Let's pause a moment and describe a refspec properly, in this particular case. (Refspecs also occur for git push but, as usual with git, implementation details leak out and they work slightly differently there.) A refspec has an optional leading plus (+) sign, which I'll ignore here;3 then two parts, separated by a colon (:). Both are often just a branch name, but you can (and fetch = lines do) spell out the "full" ref-name, refs/heads/branch in the case of a branch name.

对于获取操作,左侧的名称是远程本身的名称(如git ls-remote例如)。右侧的名称是要在本地 git 存储库中存储/更新的名称。作为特殊情况,您可以有一个星号(*) 在斜线之后作为最后一个组成部分,例如refs/heads/*,在这种情况下,左侧匹配的部分将被右侧替换。因此refs/heads/*:refs/remotes/remote-X/*是什么导致refs/heads/master(如在遥控器上看到的,git ls-remote) 成为refs/remotes/remote-X/master(如您的本地存储库中所示,以较短的形式显示在-> line git fetch印刷)。

如果你不输入:, 尽管,git fetch没有好地方可以放置“那边的分支”的副本。假设它将带来遥控器的refs/heads/master (the master远程分支)。而不是更新your refs/heads/master——如果你在分支中有自己的提交,显然那会很糟糕master-它只是将更新转储到FETCH_HEAD.

这就是事情变得特别奇怪的地方。假设你跑git fetch remote-X master branch,即给出至少一个,也许几个,refspec,但都没有冒号。

  • 如果您的 git 版本早于 1.8.4,则更新only进入FETCH_HEAD。如果你给出了两个无冒号的参考规范,FETCH_HEAD现在包含two lines:

    676699a0e0cdfd97521f3524c763222f1c30a094        branch 'master' of ...
    222c4dd303570d096f0346c3cd1dff6ea2c84f83        branch 'branch' of ...
    
  • 如果您的 git 版本是 1.8.4 或更高版本,则更新会在那里 — 这部分不变 — 但是also,fetch趁机记录这些分支永久在其适当的远程分支中,如给出的fetch =遥控器的线路。

    但无论出于什么原因,git fetch只打印出更新->实际更新的远程分支的行。自从它always记录所有更新FETCH_HEAD, it always在这里打印分支名称。

    (除了需要 git 1.8.4 或更新版本之外,更新远程分支的另一个问题是那些fetch =线路必须存在。如果不这样做,则没有映射可以让 fetch 知道重命名refs/heads/* to refs/remotes/remote-X/*.)

换句话说,git 1.8.4 及更新版本确实“伺机更新”所有远程分支。旧版本的 git 可以在git push,所以之前一直不一致。即使在 git 1.8.4 中它仍然与git pull,我认为(虽然我不使用git pull足以引起注意:-) );这应该在 git 1.9 中得到修复。

现在让我们回到两者之间的区别git fetch remote and git fetch remote refspec ....


  • 如果你跑git fetch remote,即省略所有 refspec,获取会回退到fetch =线路如常。获取操作带来了来自的所有引用fetch线。All这些进入FETCH_HEAD,但这一次它们被标记为“不可合并”(带有制表符,我将其更改为一个空格以更好地适应网页):

    676699a0e0cdfd97521f3524c763222f1c30a094 not-for-merge branch ...
    

    不是分支的引用,例如,refs/notes/带来的参考文献,请改为阅读:

    f07cf14302eab6ca614612591e55f7340708a61b not-for-merge 'refs/notes/commits' ...
    

    同时,如有必要,远程分支引用也会更新,并有消息告诉您哪些分支已更新:

       22b38d1..676699a  master     -> remote-X/master
    

    Again, 一切被倾倒到FETCH_HEAD,但仅更新和打印“需要更新”的参考。新分支打印“新分支”,旧分支打印缩写的新旧 SHA-1,至于master -> remote-X/master above.

  • If, on the other hand, you run git fetch remote refspec ..., the fetch brings over only the specified refspecs. These all go into FETCH_HEAD as usual,6 but this time every one of them is printed. Then, if your git is 1.8.4 or newer, any reference-updates that can be mapped (via sensible fetch = lines) and need updating are also updated and printed:

     * branch            master     -> FETCH_HEAD
     * branch            branch     -> FETCH_HEAD
       22b38d1..676699a  master     -> remote-X/master
    

    如果您的 git 版本早于 1.8.4,则更新remote-X/master在这种情况下不会发生,或者更确切地说,除非您的命令行参考规范之一是,否则不会发生refs/heads/master:refs/remotes/remote-X/master, or refs/heads/*:refs/remotes/remote-X/*,或前面带有加号的变体。


1This is not a great error message. The remote-X argument was never supposed to be a "repository", it was supposed to be a "remote"! It might be nice if git said something more informative here.

2There's a flaw in the git remote protocol: HEAD is usually an indirect ref as it's the current branch on the remote, so it should come over as "ref: refs/heads/master" for instance, but instead it comes over as the fully resolved SHA-1. At least one git command (git clone) attempts to "guess" the current branch on the remote by comparing this SHA-1 to that of each branch-head. In the above, for instance, it's clear that the remote is "on branch master", as HEAD and refs/heads/master have the same SHA-1. But if multiple branch names point to the same commit, and HEAD matches that commit-ID, there's no way to tell which branch (if any) HEAD is on. The remote could be in "detached HEAD" state too, in which case it's not on any branch, regardless of SHA-1 values.

2019 年编辑:此错误已在 Git 版本 1.8.4.3 中修复。只要您克隆的计算机和您自己的计算机上的两个 Git 版本都是 1.8.4.3 或更高版本,Git 就不再需要猜测。

3The plus sign means "accept forced updates", i.e., take updates that would be rejected by the "nothing but fast forward"4 rule for branches, or "never change tags"5 for tags.

4A "fast forward" for a label, changing it from an old SHA-1 to a new one, is possible when the old SHA-1 in the commit Directed Acyclic Graph is an ancestor of the new SHA-1.

5The "never change tags" rule was new in git 1.8.2. If your git is older than that, git uses the branch rules for tags too, allowing fast-forwarding without "forced update".

6But without the not-for-merge this time. Basically, when you supply colon-less refspecs, git fetch assumes they're "for merge" and puts them into FETCH_HEAD so that git merge FETCH_HEAD can find them. (I have not tested what happens with non-branch refs.)

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

git fetch 和 git fetch origin master 之间的区别 的相关文章

随机推荐

  • 在参数评估顺序中警告 UB

    我最近在这样的代码中遇到了一个错误 class C public foo return value depends on C s state AND each call to foo changes the state int foo in
  • setOnItemClickListener 没有响应[重复]

    这个问题在这里已经有答案了 可能的重复 setOnItemClickListener 没有被调用 我有一个扩展基本适配器的自定义适配器 当我在 onCreate 中设置 setOnItemClickListener 并实现 onItemCl
  • 有没有办法让 Perl 正则表达式搜索不区分大小写?

    Example my cities qr San Francisco Los Angeles 标量 cities将匹配San Francisco and Los Angeles但不会匹配SAN FRANCISCO LOS ANGELES s
  • 覆盖 Django 表单的默认属性

    在我的 Django 应用程序中 我有几种不同的表单 它们的风格相似 为了不一遍又一遍地重复自己 我尝试重写默认的表单设置 首先 我想为我在应用程序中使用的每个表单设置一些默认设置 并尝试将其子类化django forms Form cla
  • 检测传单是否放大或缩小

    我如何知道用户何时放大或缩小 如果用户在一定级别后缩放 我想启动动画 当事件zoomstart被触发时是否可以知道这一点 我如何知道用户何时放大或缩小 在每zoom水平 计算多少map getZoom 已经改变 当事件zoomstart被触
  • 无法再连接到本地 SQL Server 2008 数据库

    今天我去连接到本地数据库 但由于未知原因无法再连接 我像往常一样输入 本地 并使用 Windows 身份验证 但出现此错误 无法连接到本地主机 发生网络相关或特定于实例的错误 建立与 SQL 的连接 服务器 找不到服务器或 无法访问 验证
  • 如何更改 ServiceStack 中的默认 ContentType?

    I have 注册了新的内容类型在 ServiceStack 中 appHost ContentTypeFilters Register application x my content type SerializeToStream Des
  • C++ 中 std::cin 对象的规则是什么?

    我正在编写一个小程序供我个人使用 用于练习学习 C 及其功能 即 MLA 引文生成器 我正在写一篇包含数十次引用的大型论文 由于缺乏更好的方法来做到这一点 我不理解类或在主程序中使用其他 cpp 文件 所以不用费心告诉我 当我有更多时间时我
  • 为什么 git track 我桌面上的所有新文件?

    我有一个 问题 当我在桌面上创建 或下载 任何文件夹 文件时 Git 该轨道 这是为什么 这是正常还是不正常 O S Windows 10 Git version latest at this moment 好吧 当我打开 VS 代码时 代
  • 水平滚动网格视图

    我知道在 Android 中不可能水平滚动网格视图 但我正在做的是在水平滚动视图中动态添加图像按钮 如下所示 public class HorizontalScroller extends Activity static int l 0 p
  • 表达式混合:为什么我没有过渡和缓动选项?

    我正在使用带有 Net 3 5 的 Expression Blend 4 在下图中 有过渡效果选项以及为过渡选择缓动效果的功能 我的 Blend 界面在我的 WPF 应用程序上没有这些 但是 如果我创建一个新应用程序 例如 WPF Sket
  • 当您已经拥有主分支时,如何从存储库派生新分支?

    我已经将一个存储库分叉到我自己的 github 帐户中 并成功将其拉到我的电脑上 但是现在原始存储库上有一个我想要的新分支 但是当我尝试分叉该分支时 它会将我带到主分支在我的 github 帐户上 实际上没有做任何事情 我如何在我的帐户上获
  • 深度学习中可能/也许的类别

    我有兴趣利用我在深度学习任务中拥有的一些部分标记的数据 我使用的是完全卷积方法 而不是从标记区域中采样补丁 我有一些掩模 可以勾勒出图像中明确的正例区域 但图像中未掩模的区域不一定是负的 它们可能是正的 有谁知道如何将此类课程融入深度学习环
  • 与 SQLAlchemy、SQLite 比较日期

    我正在编写一个使用 SQLAlchemy 和 SQLite 数据库的应用程序 我相信我的数据库 表和映射配置正确 因为其他操作按预期工作 我正在尝试编写一个函数 用于检索其日期字段与作为函数参数提供的 datetime date 相匹配的所
  • JSF 应用程序中可添加书签的 URL - 尝试使用 Spring Webflow 和 JSF。有什么建议么?

    您的应用程序是 JSF hibernate 和 Spring 目前 url 的格式如下 我们想要一个干净的网址 例如http www skill guru com urltitle some范围 我们可以实现此目的的方法之一是通过将 Spr
  • 从 ASP.NET GridView 获取 DataRow

    我有一个 ASP NETGridView这绑定到一个ObjectDataSource 绑定到MySQL数据库 在这个网格上 我有 2 个未绑定的ButtonField我想要触发服务器端事件的列 因此我添加了一个事件处理程序方法GridVie
  • 如何使用 C# 打印文本文件 [关闭]

    Closed 这个问题不符合堆栈溢出指南 目前不接受答案 如何在 C 中打印文本文件 在控制台应用程序中 这是我发现的 msdn样本和这个stackoverflow 答案是msdn示例 链接中的代码适用于 Windows 窗体应用程序 不适
  • 根据时区在一天中的特定时间安排 Hangfire 作业

    在hangfire中 我可以安排一个作业在特定时间运行延迟调用方法 BackgroundJob Schedule gt Console WriteLine Hello world TimeSpan FromDays 1 我有一张包含以下信息
  • Woocommerce 中价格较低的产品的购物车折扣

    如何在产品购物车中为价格较低的产品应用折扣 例如 我的购物车中有两种产品 一种售价 150 美元 另一种售价 200 美元 我只想对成本较低的产品 在本例中为第一个产品 应用 10 的折扣 我有这个代码 但它仅适用于购物车中的第二个产品 a
  • git fetch 和 git fetch origin master 之间的区别

    I was 进行获取 合并并想知道这样做是否有什么区别 git fetch and git fetch origin master 我没有任何其他分支和起源点到我的remote repository在 GitHub 上 当我做 git fe