zoukankan      html  css  js  c++  java
  • git fetch和git pull 在使用Refspec上的区别

    假设远程分支为:master
    本地分支为:mymaster

    关于Refspec介绍请查看该链接:https://git-scm.com/book/en/v2/Git-Internals-The-Refspec
    大家一般都知道,git fetch是更新(update)在本地电脑上的远程跟踪分支(如origin/master分支,注意远程跟踪分支是保存在本地,一般在.git efs emotesorigin目录下),并更新(update) .git/FETCH_HEAD文件。并不会和本地分支merge,即不会更新本地分支。
    而git pull 相当于git fetch && git merge FETCH_HEAD, 即除了做了git fetch的工作外,还会将更新后的远程跟踪分支与本地分支merge。

    但其实有一种情况,git fetch也会更新本地分支。
    命令:git fetch origin master:mymaster
    但该命令必须严格同时满足以下两个条件:
    1.本地当前分支不能是mymaster。
    2.本地mymaster分支是origin/master的祖先。
    则该命令执行后,可以实现 本地mymaster和远程master分支进行fast-forward merge,更新了本地mymaster分支。
    只要这两个条件其中一个不满足,则执行该命令会报错!

    当满足上述两个条件时,执行命令:
    git pull origin master:mymaster
    则除了做了git fetch origin master:mymaster的工作外,还会将远程分支merge进本地当前分支。

    ps:其实当前分支是mymaster时,git pull origin master:mymaster命令仍然可以执行,因为git pull底层给git fetch加了–update-head-ok标记。但只能fast_forward merge,即本地mymaster分支必须是origin/master的祖先, 否则会报 ![rejected](non-fast-forward)。
    注:
    -u
    –update-head-ok
    By default git fetch refuses to update the head which corresponds to the current branch. This flag disables the check. This is purely for the internal use for git pull to communicate with git fetch, and unless you are implementing your own Porcelain you are not supposed to use it.

    下面介绍下另外一条命令:git fetch origin master
    这命令其实就是:git fetch origin master:
    Refspec省略了目的分支(即本地分支)。
    该命令不需要满足前述“git fetch origin master:mymaster” 中的两个条件都可以执行。
    但该命令在git 1.8.4(August 2013)版本之前,只会更新(update) FETCH_HEAD,而不会更新(update)远程跟踪分支(即origin/master)。在git 1.8.4之后,除了会更新(update) FETCH_HEAD外,还会更新(update)远程跟踪分支了。(参考:https://github.com/git/git/commit/f269048754f3b835f4f7287c5a132714a059efce)
    而命令:git pull origin master
    除了做了“git fetch origin master”的工作外,还会执行:
    git merge FETCH_HEAD
    注:
    The 1.8.4 release notes mention that:
    “git fetch origin master” unlike “git fetch origin” or “git fetch”
    did not update “refs/remotes/origin/master”; this was an early
    design decision to keep the update of remote tracking branches
    predictable, but in practice it turns out that people find it more
    convenient to opportunistically update them whenever we have a
    chance, and we have been updating them when we run “git push” which
    already breaks the original “predictability” anyway.

    总结:
    当前分支是mymaster分支时,git fetch origin master:mymaster会报fatal,不会执行;而git fetch origin master可以执行,更新远程跟踪分支origin/master(git 1.8.4版本后)和FETCH_HEAD。
    当前分支不是mymaster分支时,git fetch origin master:mymaster会执行,更新远程跟踪分支origin/master(git 1.8.4版本前后都会更新)和FETCH_HEAD,假如mymaster分支是origin/master的祖先,就会实现本地mymaster和远程master分支进行fast-forward merge,从而更新了本地mymaster分支;假如mymaster分支不是origin/master的祖先,会报![rejected](non-fast-forward);但git fetch origin master仍然可以执行,更新远程跟踪分支origin/master(git 1.8.4版本后)和FETCH_HEAD,不会对本地mymaster分支有任何影响。

    补充:
    如何从远程库中只fetch单个分支下来,比如只fetch 远程库中的master分支(即只更新FETCH_HEAD和远程跟踪分支origin/master)?(如果远程库的其他分支非常大,只fetch单个分支将大大提高fetch速度)
    在git 1.8.4版本之前:
    方法一(这里mymaster分支为新建分支):
    1.git fetch origin master
    2.git branch mymaster FETCH_HEAD
    3.git checkout mymaster
    注:此方法在git 1.8.4版本之前其实并没有更新远程跟踪分支origin/master,只是mymaster与远程分支master同步了。
    方法二:
    git fetch origin master:remotes/origin/master 或者
    git fetch origin master:refs/remotes/origin/master
    注意不能:git fetch origin master:origin/master,不然将会建立新的本地分支:origin/master

    在git 1.8.4版本之后,除了以上方法,还可以直接:
    git fetch origin master

  • 相关阅读:
    非监督学习
    4.5_岭回归案例分析
    4.4_回归算法之岭回归
    4.3_回归性能评估与欠拟合|过拟合
    4.2_线性回归案例分析
    回归算法
    HDU 2105 The Center of Gravity (数学)
    HDU 2089 不要62 (数学)
    HDU 2036 改革春风吹满地 (数学)
    HDU 1840 Equations (数学)
  • 原文地址:https://www.cnblogs.com/a3192048/p/12241240.html
Copyright © 2011-2022 走看看