Git fetch和git pull的区别

Git fetch和git pull的区别


Qyouu
浏览 728回答 2
2回答

ibeautiful

每一个本地库下都有一个.git的隐藏文件夹,文件夹中的文件保存着跟这个本地库相关的信息首先来看下其中的config文件[plain] view plain copy[core]repositoryformatversion = 0filemode = falsebare = falselogallrefupdates = truesymlinks = falseignorecase = truehideDotFiles = dotGitOnly[remote "origin"]url = git@github.com:seanzou88/fetch.gitfetch = +refs/heads/*:refs/remotes/origin/*[branch "master"]remote = originmerge = refs/heads/master从这个文件中我们可以了解到:1,本地库的当前分支为master,其关联的远程库名称为origin(不同的名称可以指向同一个远程库,参见git remote命令)2,远程库origin所在的位置为(URL):git@github.com:seanzou88/fetch.git然后可以查看.git文件夹下的HEAD文件:[plain] view plain copyref: refs/heads/master其指向.git\refs\heads\master文件[plain] view plain copyce71505b3626a3648b2c32ea2081d65049cad300这个文件中保存的是本地库中最新的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为:ce71505b3626a3648b2c32ea2081d65049cad300githup上的latest commit id为:ab8cd391f978fe5384a78c92001ef8ae861046f0before:.git\refs\heads\master[plain] view plain copyce71505b3626a3648b2c32ea2081d65049cad300.git\refs\remotes\origin\master[plain] view plain copyce71505b3626a3648b2c32ea2081d65049cad300.git\logs\refs\heads\master[plain] view plain copy0000000000000000000000000000000000000000ce71505b3626a3648b2c32ea2081d65049cad300......commit (initial): first commit.git\logs\refs\remotes\origin\master[plain] view plain copy0000000000000000000000000000000000000000ce71505b3626a3648b2c32ea2081d65049cad300......update by pushafter:.git\refs\heads\master(不变).git\refs\remotes\origin\master[plain] view plain copyab8cd391f978fe5384a78c92001ef8ae861046f0.git\logs\refs\heads\master(不变).git\logs\refs\remotes\origin\master[plain] view plain copy0000000000000000000000000000000000000000ce71505b3626a3648b2c32ea2081d65049cad300......update by pushce71505b3626a3648b2c32ea2081d65049cad300ab8cd391f978fe5384a78c92001ef8ae861046f0......fetch origin: fast-forward本地库并没有变化,也就是说,git fetch只会将本地库所关联的远程库的commit id更新至最新HEAD没有变化很容易理解,因为本地库并没有变化git pull origin master:master本地的latest commit id为:3643a1a65fc88ae0e9f28f12168629758d027415githup上的latest commit id为:64df093f73294d82a3adce9694871b9fac2aecfbbefore:.git\refs\heads\master[plain] view plain copy3643a1a65fc88ae0e9f28f12168629758d027415.git\refs\remotes\origin\master[plain] view plain copy3643a1a65fc88ae0e9f28f12168629758d027415.git\logs\refs\heads\master[plain] view plain copy00000000000000000000000000000000000000003643a1a65fc88ae0e9f28f12168629758d027415......commit (initial): first commit.git\logs\refs\remotes\origin\master[plain] view plain copy00000000000000000000000000000000000000003643a1a65fc88ae0e9f28f12168629758d027415......update by pushafter:.git\refs\heads\master[plain] view plain copy64df093f73294d82a3adce9694871b9fac2aecfb.git\refs\remotes\origin\master(不变).git\logs\refs\heads\master[plain] view plain copy00000000000000000000000000000000000000003643a1a65fc88ae0e9f28f12168629758d027415......commit (initial): first commit3643a1a65fc88ae0e9f28f12168629758d02741564df093f73294d82a3adce9694871b9fac2aecfb......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中从远程的分支获取最新的版本到本地有这样2个命令:1. git fetch:相当于是从远程获取最新版本到本地,不会自动mergegit fetch origin mastergit log -p master..origin/mastergit merge origin/master以上命令的含义:首先从远程的origin的master主分支下载最新的版本到origin/master分支上然后比较本地的master分支和origin/master分支的差别最后进行合并上述过程其实可以用以下更清晰的方式来进行:git fetch origin master:tmpgit diff tmpgit merge tmp从远程获取最新的版本到本地的test分支上之后再进行比较合并2. git pull:相当于是从远程获取最新版本并merge到本地git pull origin master上述命令其实相当于git fetch 和 git merge在实际使用中,git fetch更安全一些因为在merge前,我们可以查看更新情况,然后再决定是否合并
打开App,查看更多内容
随时随地看视频慕课网APP