转自: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. Android中使用AlertDialog实现几种不同的对话框

    场景 app中常见的对话框. 简单的带确定取消按钮的对话框 带列表的对话框 带单项选择的对话框 带多项选择的对话框 注: 博客: https://blog.csdn.net/badao_liumang ...

  2. AndroidStudio中使用XML和Java代码混合控制UI界面实现QQ相册照片列表页面

    场景 效果 注: 博客: https://blog.csdn.net/badao_liumang_qizhi 关注公众号 霸道的程序猿 获取编程相关电子书.教程推送与免费下载. 实现 新建Androi ...

  3. 关于使用kms时遇到的there is nothing to do here

    我讲一下我自己的解决方法 ------------------------------------ 这个错误还是很明显的,没事干,如果不是kms的锅的话,那么就是字面上的问题了 ----------- ...

  4. C#中的WinFrom技术实现串口通讯助手(附源码)

    C#中的WinFrom技术实现串口通讯助手(附源码)   实现的功能: 1.实现自动加载可用串口. 2.打开串口,并且使用C#状态栏显示串口的状态. 3.实现了串口的接收数据和发送数据功能. 4.串口 ...

  5. 图片-定义select向下箭头样式

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  6. 优酷爱奇艺视频转换为MP4格式工具

    本君今天分享两个免费的视频格式转换工具,分别是爱奇艺和优酷的(腾讯的有点复杂,等整理完再分享).教程都是一步步亲手操作的,每一步都有配图.希望各位老板多转发分享,谢谢! 一爱奇艺QSV转MP4格式 解 ...

  7. javascript 基础整理

    js编码标准 参考 数据类型 注意事项

  8. 安装Mailx到CentOS(YUM)

    运行环境 系统版本:CentOS Linux release 7.3.1611 (Core) 软件版本:无 硬件版本:无 安装过程 1.配置网络 [root@localhost ~]# vi /etc ...

  9. jQuery - lable 取值、赋值

    取值 var val = $("#breakfastMonday").html(); 赋值 $("#breakfastMonday").html("v ...

  10. vsftp配置遇到的一些问题

    设置匿名登陆的时候,要保证 /var/ftp/ 的所有者 是root,不然会一直提示输入用户名和密码,无法登陆! 上传的权限 local_umask =002 以及 匿名用户 anon_umask=0 ...