当前位置: 首页 > news >正文

git fetch和git pull之间的区别--转载

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

 

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

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

首先来看下其中的config文件

[plain]  view plain copy 在CODE上查看代码片 派生到我的代码片
 
  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文件:

[plain]  view plain copy 在CODE上查看代码片 派生到我的代码片
 
  1. ref: refs/heads/master  

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

[plain]  view plain copy 在CODE上查看代码片 派生到我的代码片
 
  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

[plain]  view plain copy 在CODE上查看代码片 派生到我的代码片
 
  1. ce71505b3626a3648b2c32ea2081d65049cad300  

.git\refs\remotes\origin\master

[plain]  view plain copy 在CODE上查看代码片 派生到我的代码片
 
  1. ce71505b3626a3648b2c32ea2081d65049cad300  

.git\logs\refs\heads\master

[plain]  view plain copy 在CODE上查看代码片 派生到我的代码片
 
  1. 0000000000000000000000000000000000000000   
  2.         ce71505b3626a3648b2c32ea2081d65049cad300   
  3.         ......    
  4.         commit (initial): first commit   

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

[plain]  view plain copy 在CODE上查看代码片 派生到我的代码片
 
  1. 0000000000000000000000000000000000000000   
  2.         ce71505b3626a3648b2c32ea2081d65049cad300   
  3.         ......    
  4.         update by push  

after:

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

.git\refs\remotes\origin\master

[plain]  view plain copy 在CODE上查看代码片 派生到我的代码片
 
  1. ab8cd391f978fe5384a78c92001ef8ae861046f0  

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

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

[plain]  view plain copy 在CODE上查看代码片 派生到我的代码片
 
  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

[plain]  view plain copy 在CODE上查看代码片 派生到我的代码片
 
  1. 3643a1a65fc88ae0e9f28f12168629758d027415  

.git\refs\remotes\origin\master

[plain]  view plain copy 在CODE上查看代码片 派生到我的代码片
 
  1. 3643a1a65fc88ae0e9f28f12168629758d027415  

.git\logs\refs\heads\master

[plain]  view plain copy 在CODE上查看代码片 派生到我的代码片
 
  1. 0000000000000000000000000000000000000000   
  2.         3643a1a65fc88ae0e9f28f12168629758d027415   
  3.         ......    
  4.         commit (initial): first commit  

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

[plain]  view plain copy 在CODE上查看代码片 派生到我的代码片
 
  1. 0000000000000000000000000000000000000000   
  2.         3643a1a65fc88ae0e9f28f12168629758d027415   
  3.         ......    
  4.         update by push  

after:

.git\refs\heads\master

[plain]  view plain copy 在CODE上查看代码片 派生到我的代码片
 
  1. 64df093f73294d82a3adce9694871b9fac2aecfb  

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

.git\logs\refs\heads\master

[plain]  view plain copy 在CODE上查看代码片 派生到我的代码片
 
  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来看,实现上不是这样实现的

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

 

相关文章:

  • centos 零碎学习小记 9.
  • Android中的windowSoftInputMode属性详解
  • 手机看:用例图
  • nginx实现负载均衡
  • Makefile文件的编写(1)
  • CDH使用之CM、CDH4、5卸载
  • 小菜学设计模式——单一职责原则
  • 算法-最大公约数
  • Androd开发之广告栏设计
  • nginx+lua+redis(openresty)配置
  • 模拟 2015百度之星资格赛 1003 IP聚合
  • 增值税发票管理解决方案
  • SQL Server利用RowNumber()内置函数与Over关键字实现通用分页存储过程(支持单表或多表结查集分页)...
  • Ubuntu 下 Mysql 新建数据库和用户
  • 运维角度浅谈MySQL数据库优化
  • 【347天】每日项目总结系列085(2018.01.18)
  • C++类的相互关联
  • ComponentOne 2017 V2版本正式发布
  • ECMAScript6(0):ES6简明参考手册
  • Java方法详解
  • js如何打印object对象
  • laravel with 查询列表限制条数
  • mysql 数据库四种事务隔离级别
  • python 装饰器(一)
  • Python进阶细节
  • Spring Boot快速入门(一):Hello Spring Boot
  • Vue2.x学习三:事件处理生命周期钩子
  • vue从创建到完整的饿了么(11)组件的使用(svg图标及watch的简单使用)
  • Webpack入门之遇到的那些坑,系列示例Demo
  • 开源中国专访:Chameleon原理首发,其它跨多端统一框架都是假的?
  • 利用jquery编写加法运算验证码
  • 强力优化Rancher k8s中国区的使用体验
  • 使用 Node.js 的 nodemailer 模块发送邮件(支持 QQ、163 等、支持附件)
  • 推荐一个React的管理后台框架
  • 小程序01:wepy框架整合iview webapp UI
  • AI又要和人类“对打”,Deepmind宣布《星战Ⅱ》即将开始 ...
  • 阿里云服务器如何修改远程端口?
  • 策略 : 一文教你成为人工智能(AI)领域专家
  • ​软考-高级-系统架构设计师教程(清华第2版)【第9章 软件可靠性基础知识(P320~344)-思维导图】​
  • #android不同版本废弃api,新api。
  • #Linux(帮助手册)
  • $.ajax()方法详解
  • (a /b)*c的值
  • (done) NLP “bag-of-words“ 方法 (带有二元分类和多元分类两个例子)词袋模型、BoW
  • (pojstep1.3.1)1017(构造法模拟)
  • (pytorch进阶之路)CLIP模型 实现图像多模态检索任务
  • (附源码)ssm高校实验室 毕业设计 800008
  • (六)库存超卖案例实战——使用mysql分布式锁解决“超卖”问题
  • (一)VirtualBox安装增强功能
  • (一)搭建springboot+vue前后端分离项目--前端vue搭建
  • (已解决)什么是vue导航守卫
  • (原創) 物件導向與老子思想 (OO)
  • (转)scrum常见工具列表
  • (轉貼) 寄發紅帖基本原則(教育部禮儀司頒布) (雜項)
  • .NET 8 中引入新的 IHostedLifecycleService 接口 实现定时任务