ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

git fetch和git pull对比

2022-07-25 01:03:08  阅读:222  来源: 互联网

标签:pull HEAD git fetch merge FETCH


情景重现

你:面试官您好,我是xxx,毕业于xxx学校,工作xxx年,精通各种git命令。

面试官:您好您好,我问个常见的问题考察一下您的技术水平哈。请问,git pull和git fetch有什么区别,二者都在什么情况下使用?

你:emmmm...母鸡喔

面试官(微笑):回家等消息吧,有结果通知你~

 

二者区分

git fetch

首先我们来看一下git fetch的定义:

git fetch only downloads latest changes into the local repository. 
It downloads fresh changes that other developers have pushed to 
the remote repository since the late fetch and allows you to review 
and merge manually at a later time using git merge. 
Because it doesn't change your working directory or the staging area, 
it is entirely safe, and you can run it as often as you want.

我们提取几个关键信息:

  1. git fetch提取远端的最新改变到本地

  2. git fetch不强行改变本地状态和预存区

  3. 可使用git merge手动合并

  4. git fetch很安全

 

git pull

我们再看一下git pull的定义:

git pull downloads latest changes into the local repository 
and it also automatically merges change in your working directory. 
It doesn't give you a chance to review the changes before merging, 
and as a consequence, 'merge conflicts' can and do occur. One important thing
to keep in mind is that it will merge only into the current working branch.
Other branches will stay unaffected.

我们再提取几个关键信息:

  1. git pull直接将改动同步到本地工作区

  2. git pull没机会做merge操作

  3. git pull可能出现merge冲突

  4. 最好在同分支之间使用git pull,否则会出错误

总之:git pull = git fetch + git merge

 

二者在git中的位置

image
 

git fetch使用

  1. 获取远端分支的最新内容到FETCH_HEAD,并查看。
git fetch origin master
git log -p FETCH_HEAD
  1. 如果可以合并,就合并内容到本地
git merge

这里解释一下这个FETCH_HEAD, 它是一个版本链接,记录在本地一个文件中(.git/FETCH_HEAD),指向当前分支最新版本。

 

git pull使用

git pull的使用相当于上面两步的和,

git fetch origin master
git merge FETCH_HEAD

 

总结

此题不难,但答不上来的侮辱性极强。

本题主要考察的是git的基础知识,多看一些博客或者文章就可以搞定。

 

鸣谢

git fetch vs git pull

git官网

git fetch & pull详解

标签:pull,HEAD,git,fetch,merge,FETCH
来源: https://www.cnblogs.com/young233/p/16516033.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有