原文地址:http://blog.csdn.net/a19881029/article/details/42245955

git fetch和git pull都可以用来更新本地库,它们之间有什么区别呢?

每一个本地库下都有一个.git的隐藏文件夹,文件夹中的文件保存着跟这个本地库相关的信息

首先来看下其中的config文件

  1. [core]
  2. repositoryformatversion = 0
  3. filemode = false
  4. bare = false
  5. logallrefupdates = true
  6. symlinks = false
  7. ignorecase = true
  8. hideDotFiles = dotGitOnly
  9. [remote "origin"]
  10. url = git@github.com:seanzou88/fetch.git
  11. fetch = +refs/heads/*:refs/remotes/origin/*
  12. [branch "master"]
  13. remote = origin
  14. merge = refs/heads/master

从这个文件中我们可以了解到:

1,本地库的当前分支为master,其关联的远程库名称为origin(不同的名称可以指向同一个远程库,参见git remote命令)

2,远程库origin所在的位置为(URL):git@github.com:seanzou88/fetch.git

然后可以查看.git文件夹下的HEAD文件:

  1. ref: refs/heads/master

其指向.git\refs\heads\master文件

  1. ce71505b3626a3648b2c32ea2081d65049cad300

这个文件中保存的是本地库中最新的commit id

.git\refs文件夹很有意思,面分为3个文件夹

heads文件夹前面说过了

remotes文件夹中的每一个文件夹代表一个远程库名称(git remote),其中的每个文件关联远程库的一个分支,其中保存该分支的最新commit id

.git\logs文件夹下保存的是.git\refs文件夹下相应文件的变更记录

准备工作到此结束,下面可以具体看看git fetch和git pull之间的区别了

git fetch origin

本地的latest commit id为:ce71505b3626a3648b2c32ea2081d65049cad300

githup上的latest commit id为:ab8cd391f978fe5384a78c92001ef8ae861046f0

before:

.git\refs\heads\master

  1. ce71505b3626a3648b2c32ea2081d65049cad300

.git\refs\remotes\origin\master

  1. ce71505b3626a3648b2c32ea2081d65049cad300

.git\logs\refs\heads\master

  1. 0000000000000000000000000000000000000000
  2. ce71505b3626a3648b2c32ea2081d65049cad300
  3. ......
  4. commit (initial): first commit

.git\logs\refs\remotes\origin\master

  1. 0000000000000000000000000000000000000000
  2. ce71505b3626a3648b2c32ea2081d65049cad300
  3. ......
  4. update by push

after:

.git\refs\heads\master(不变)

.git\refs\remotes\origin\master

  1. ab8cd391f978fe5384a78c92001ef8ae861046f0

.git\logs\refs\heads\master(不变)

.git\logs\refs\remotes\origin\master

  1. 0000000000000000000000000000000000000000
  2. ce71505b3626a3648b2c32ea2081d65049cad300
  3. ......
  4. update by push
  5. ce71505b3626a3648b2c32ea2081d65049cad300
  6. ab8cd391f978fe5384a78c92001ef8ae861046f0
  7. ......
  8. fetch origin: fast-forward

本地库并没有变化,也就是说,git fetch只会将本地库所关联的远程库的commit id更新至最新

HEAD没有变化很容易理解,因为本地库并没有变化

git pull origin master:master

本地的latest commit id为:3643a1a65fc88ae0e9f28f12168629758d027415

githup上的latest commit id为:64df093f73294d82a3adce9694871b9fac2aecfb

before:

.git\refs\heads\master

  1. 3643a1a65fc88ae0e9f28f12168629758d027415

.git\refs\remotes\origin\master

  1. 3643a1a65fc88ae0e9f28f12168629758d027415

.git\logs\refs\heads\master

  1. 0000000000000000000000000000000000000000
  2. 3643a1a65fc88ae0e9f28f12168629758d027415
  3. ......
  4. commit (initial): first commit

.git\logs\refs\remotes\origin\master

  1. 0000000000000000000000000000000000000000
  2. 3643a1a65fc88ae0e9f28f12168629758d027415
  3. ......
  4. update by push

after:

.git\refs\heads\master

  1. 64df093f73294d82a3adce9694871b9fac2aecfb

.git\refs\remotes\origin\master(不变)

.git\logs\refs\heads\master

  1. 0000000000000000000000000000000000000000
  2. 3643a1a65fc88ae0e9f28f12168629758d027415
  3. ......
  4. commit (initial): first commit
  5. 3643a1a65fc88ae0e9f28f12168629758d027415
  6. 64df093f73294d82a3adce9694871b9fac2aecfb
  7. ......
  8. pull origin master:master: fast-forward

.git\logs\refs\remotes\origin\master(不变)

本地库更新至最新,git pull会将本地库更新至远程库的最新状态

由于本地库进行了更新,HEAD也会相应的指向最新的commit id

所以虽然从结果上来看,git pull = git fetch + git merge,但是从文件中保存的commit id来看,实现上不是这样实现的

为了更好的理解,画了个图:

git fetch和git pull之间的区别--转载的更多相关文章

  1. 详解git fetch与git pull的区别(实操)

    感谢原文作者:R-H-R 原文链接:https://blog.csdn.net/riddle1981/article/details/74938111 git fetch和git pull都可以将远端 ...

  2. Git fetch和git pull的区别, 解决Git报错:error: You have not concluded your merge (MERGE_HEAD exists).

    Git fetch和git pull的区别, 解决Git报错:error: You have not concluded your merge (MERGE_HEAD exists). Git fet ...

  3. get merge --no-ff和git merge区别、git fetch和git pull的区别

    get merge --no-ff和git merge区别 git merge -–no-ff可以保存你之前的分支历史.能够更好的查看 merge历史,以及branch 状态. git merge则不 ...

  4. git:Git fetch和git pull的区别, 解决Git报错:error: You have not concluded your merge (MERGE_HEAD exists).

    Git fetch和git pull的区别, 解决Git报错:error: You have not concluded your merge (MERGE_HEAD exists). 解决办法一:保 ...

  5. git pull、git fetch、git merge、git rebase的区别

    一.git pull与git fetch区别 1.两者的区别       两者都是更新远程仓库代码到本地. git fetch相当于是从远程获取最新版本到本地,不会自动merge. 只是将远程仓库最新 ...

  6. Git fetch和git pull的区别

    Git中从远程的分支获取最新的版本到本地有这样2个命令:1. git fetch:相当于是从远程获取最新版本到本地,不会自动merge git fetch origin mastergit log - ...

  7. Git fetch和git pull的区别(转)

    原文: http://www.tech126.com/git-fetch-pull/ Git中从远程的分支获取最新的版本到本地有这样2个命令:1. git fetch:相当于是从远程获取最新版本到本地 ...

  8. 关于git fetch 和git pull 的区别

    1.fetch 相当于是从远程获取最新版本呢到本地,不会自动merge. git fetch origin master:tmpgit diff tmp git merge tmp 2. git pu ...

  9. [Git] Git fetch和git pull的区别

    reference : http://blog.csdn.net/hudashi/article/details/7664457 Git中从远程的分支获取最新的版本到本地有这样2个命令:1. git ...

随机推荐

  1. PHP 碎片

    1. $_SERVER['REMOTE_ADDR'] cannot be modified by the user or via HTTP so you CAN trust it. -- 用这个可以有 ...

  2. 非官方的iOS设计指南

    非官方的iOS设计指南 有时候为iOS设计app并不是一件简单的事,但是如果你能找到正确的最新的苹果设备信息,并按照正确的方向,那么为iOS设计app或许会变得简单容易些. 关于这些指南 这些指南描述 ...

  3. HTML5边玩边学(1)画布实现方法

    一.<canvas>标签 Html5 引入了一个新的 <canvas> 标签,这个标签所代表的区域就好象一块画布,你的所有图形绘制最后都要在这块画布上呈现.有了这个标签,浏览器 ...

  4. C#反射实例化类并调用类的方法

    反射提高了程序的灵活性和扩展性,降低耦合性,提高自适应能力. 它允许程序创建和控制任何类的对象,无需提前硬编码目标类: SalBLL a = (SalBLL)Assembly.Load("B ...

  5. printf函数重定向

    printf函数底层会调用fputc函数 /*重定向c库函数printf到USART1*/ int fputc(int ch, FILE *f) { /*发送一个字节数据USART1 */ USART ...

  6. HDOJ_1010 Tempter of the Bone

    http://acm.hdu.edu.cn/showproblem.php?pid=1010 奇偶剪枝:可以把map看成这样: 0 1 0 1 0 1 1 0 1 0 1 0 0 1 0 1 0 1 ...

  7. sc7731 Android 5.1 LCD驱动简明笔记之一

    基于展讯sc7731 - Android 5.1 代码分析浏览.将屏蔽细节,把握整体,并且不涉及其他设备和LCD的交互. 以下对sc7731 lcd大体流程进行简要说明. 第一,lcd 的两个阶段 1 ...

  8. TMS3705A PCF7991AT 线路图

  9. Java学习笔记之方法重载,动态方法调度和抽象类

    一.方法重载 如果子类中的方法与它的超类中的方法有相同的方法名,则称子类中的方法重载超类中的方法,特别是当超类和子类中的方法名和参数类型都相同时,在子类中调用该方法时,超类中的方法会被隐藏.考虑下面程 ...

  10. 【M27】要求或者禁止对象产生于heap之中

    1.要求对象只能产生于heap之中,该怎么办? 栈上的对象肯定调用构造方法和析构方法(离开作用域的时候),因此,要求对象只能产生于heap之中,也就是禁止栈上产生对象,解决办法有两种:将所有的构造方法 ...