转自: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 + merge 和 git pull 的区别的更多相关文章

  1. git pull VS git fetch&merge

    使用git fetch和git pull都可以更新远程仓库的代码到本地,但是它们之间还是有区别. git fetch  git fetch origin master git log -p maste ...

  2. git pull VS git fetch&merge(good)

    从图中可以看到,git fetch和git pull的区别, git fetch 不会自动的将结果merge到本地,只是将远程版本同步到本地版本库,而不会merge到本地副本. git pull  将 ...

  3. git fetch, merge, pull, push需要注意的地方(转)

    在git操作中,我们经常会用到fetch, merge, pull和push等命令,以下是一些我们需要注意的地方. 给大家准备了参考资料: 1. Whatʼs a Fast Forward Merge ...

  4. git fetch, merge, pull, push需要注意的地方

    在git操作中,我们经常会用到fetch, merge, pull和push等命令,以下是一些我们需要注意的地方. 给大家准备了参考资料: 1. Whatʼs a Fast Forward Merge ...

  5. How to get started with GIT and work with GIT Remote Repo

    https://www.ntu.edu.sg/home/ehchua/programming/howto/Git_HowTo.html#zz-7. 1.  Introduction GIT is a ...

  6. git pull和git fetch的区别

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

  7. Git fetch和git pull的区别

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

  8. [转] git fetch与pull

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

  9. git fetch和git pull(转载)

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

随机推荐

  1. MySQL真正的UTF-8字符集utf8mb4

    MySQL有个utf-8的坑 MySQL 的 utf8 实际上不是真正的 UTF-8.utf8 只支持每个字符最多三个字节,而真正的 UTF-8 是每个字符最多四个字节. MySQL 一直没有修复这个 ...

  2. nginx基础(一)

    一.nginx的安装.启动.停止及文件解读 yum -y install gcc gcc-c++ autoconf pcre-devel make automake yum -y install wg ...

  3. ELK学习005:Kibana 安装与运行

    下载安装Kibana 1. 下载地址:https://www.elastic.co/cn/downloads/kibana 2. 解压下载的压缩包 [root@localhost ~]# tar -z ...

  4. 第四次oo博客作业

    (1)本单元是撰写UML数据分析器,架构大致如下,在指导书要求的函数外,对于UmlClass类,Umlinterface类,以及状态机,顺序图这四个类重现构造一个类,这个类里有他们所需要的全部信息,另 ...

  5. 最新咕咆+鲁班+图灵+享学+蚂蚁+硅谷+源码 Java架构师资料《Java架构师VIP课程》

    最新的Java架构师完整资料,完整视频+源码+文档. 每一套都是一百多个G的资料,无密. JAVA架构师全套课程 咕泡学院互联网架构师第一期 咕泡学院互联网架构师第二期 咕泡学院互联网架构师第三期 博 ...

  6. linux centos7安装mysql8

    一.RPM版安装 查看是否有其他版本的数据库,若有,删除干净 非root用户必须要有sudo权限 1.下载mysql相关安装包 https://mirrors.tuna.tsinghua.edu.cn ...

  7. MongoDB3.4版本新增特性

    先说明一下mongod和mongos的含义:mongod是MongoDB系统的主要后台进程,它处理数据请求.管理数据访问和执行后台管理操作:该命令的命令行选项主要用于测试,在场景操作中,使用配置文件选 ...

  8. instanceof读解

    function instance(l,r){ let 0 = r.prototype; let v = l.__proto__; while(true){ if(v === null){ retur ...

  9. QingTing.Fm-WPF是调用蜻蜓FMAPI 查询API内容展示,进行播放

    QingTing.Fm 是调用蜻蜓FM   API 查询界面内容,进行在线播放. Release地址下载 环境 Visual Studio 2019,dotNet Framework 4.6.1 SD ...

  10. Android实战项目——家庭记账本设计思路

    经过三周左右的Android学习,实感只有上手开发才能有所提高.在此打算做一个家庭记账APP,同时巩固一下学到的东西并且弥补漏洞. 概述 记账是自古以来人类必不可少的一件事,从古代的算盘,到手写账本, ...