转自: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. 使用 Apache James 3.3.0(开源免费) 搭建内网电子邮件服务器(基于 Windows + Amazon Corretto 8)

    电子邮件服务器,对于很多公司,都是需要的. 虽然现在很多人,使用 QQ .微信进行一对一的工作沟通,使用QQ 群.微信群进行多人沟通,但这些即时聊天工具,与电子邮件相比,仍有很多不足: a. 电子邮件 ...

  2. 疫情之下,使用FRP实现内网穿透,远程连接公司电脑进行办公

    当前情况下,经常会有需要到公司电脑进行一些操作,比如连接内网OA,数据库或者提交文档.为了减少外出,将使用frp进行内网穿透的方法进行一个说明. 前提条件 1. 一台拥有公网 IP 的设备(如果没有, ...

  3. 【HDU - 1087 】Super Jumping! Jumping! Jumping! (简单dp)

    Super Jumping! Jumping! Jumping! 搬中文ing Descriptions: wsw成功的在zzq的帮助下获得了与小姐姐约会的机会,同时也不用担心wls会发现了,可是如何 ...

  4. 什么是AOP面向切面编程思想

    一.什么是AOP? 1.AOP不是一种语言,是一种编程范式 常见的编程范式: 面向过程.面向对象.函数式编程.事件驱动编程等 2.AOP可以解决特定问题,不能解决所有问题. 3.是面向对象的补充,不是 ...

  5. java Reflection(反射)基础知识讲解

    原文链接:小ben马的java Reflection(反射)基础知识讲解 1.获取Class对象的方式 1.1)使用 "Class#forName" public static C ...

  6. 如何实现广告响应式滚动 OwlCarousel2

    githu    https://github.com/OwlCarousel2/OwlCarousel2 OwlCarousel2 官方网址    http://owlcarousel2.githu ...

  7. workerman离线推送方案

    方案一:目前网上比较流行的 方案二:参考腾迅IM

  8. Java垃圾回收手册翻译 - 什么是垃圾回收

    Java垃圾回收手册翻译 - 什么是垃圾回收 初看之下,垃圾回收应该要做其名称之事 - 找到和丢掉垃圾.然而事实上它正好做着相反的事,垃圾回收会记录所有仍在使用中的对象,然后将其他标记为垃圾.谨记这点 ...

  9. springboot整合WebService简单版

    一.什么是webservice 关于webservice的介绍摘自百度百科,上面的介绍很详细.(链接:https://baike.baidu.com/item/Web%20Service/121503 ...

  10. rhel加载raid卡驱动安装系统

    有时候需要把系统安装到RAID上,但是系统本身又缺少该RAID卡驱动,就会导致到硬盘分区时提示没有发现可用磁盘,这时我们就需要首先加载该RAID卡驱动,从而让系统识别到要使用的磁盘. RHEL5 和 ...