git fetch和git pull之间的区别 - Go语言中文社区

git fetch和git pull之间的区别


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

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

首先来看下其中的config文件

[core]
	repositoryformatversion = 0
	filemode = false
	bare = false
	logallrefupdates = true
	symlinks = false
	ignorecase = true
	hideDotFiles = dotGitOnly
[remote "origin"]
	url = git@github.com:seanzou88/fetch.git
	fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
	remote = origin
	merge = refs/heads/master

从这个文件中我们可以了解到:

1,本地库的当前分支为master,其关联的远程库名称为origin(不同的名称可以指向同一个远程库,参见git remote命令)

2,远程库origin所在的位置为(URL):git@github.com:seanzou88/fetch.git

然后可以查看.git文件夹下的HEAD文件:

ref: refs/heads/master

其指向.gitrefsheadsmaster文件

ce71505b3626a3648b2c32ea2081d65049cad300

这个文件中保存的是本地库中最新的commit id

.gitrefs文件夹很有意思,面分为3个文件夹

heads文件夹前面说过了

remotes文件夹中的每一个文件夹代表一个远程库名称(git remote),其中的每个文件关联远程库的一个分支,其中保存该分支的最新commit id

.gitlogs文件夹下保存的是.gitrefs文件夹下相应文件的变更记录

准备工作到此结束,下面可以具体看看git fetch和git pull之间的区别了

 

git fetch origin

本地的latest commit id为:ce71505b3626a3648b2c32ea2081d65049cad300

githup上的latest commit id为:ab8cd391f978fe5384a78c92001ef8ae861046f0

before:

.gitrefsheadsmaster

ce71505b3626a3648b2c32ea2081d65049cad300

.gitrefsremotesoriginmaster

ce71505b3626a3648b2c32ea2081d65049cad300

.gitlogsrefsheadsmaster

0000000000000000000000000000000000000000 
		ce71505b3626a3648b2c32ea2081d65049cad300 
		......	
		commit (initial): first commit 

.gitlogsrefsremotesoriginmaster

0000000000000000000000000000000000000000 
		ce71505b3626a3648b2c32ea2081d65049cad300 
		......	
		update by push

after:

.gitrefsheadsmaster(不变)

.gitrefsremotesoriginmaster

ab8cd391f978fe5384a78c92001ef8ae861046f0

.gitlogsrefsheadsmaster(不变)

.gitlogsrefsremotesoriginmaster

0000000000000000000000000000000000000000 
		ce71505b3626a3648b2c32ea2081d65049cad300 
		......		
		update by push
ce71505b3626a3648b2c32ea2081d65049cad300 
		ab8cd391f978fe5384a78c92001ef8ae861046f0 
		......	
		fetch origin: fast-forward

本地库并没有变化,也就是说,git fetch只会将本地库所关联的远程库的commit id更新至最新

HEAD没有变化很容易理解,因为本地库并没有变化

 

git pull origin master:master

本地的latest commit id为:3643a1a65fc88ae0e9f28f12168629758d027415

githup上的latest commit id为:64df093f73294d82a3adce9694871b9fac2aecfb

before:

.gitrefsheadsmaster

3643a1a65fc88ae0e9f28f12168629758d027415

.gitrefsremotesoriginmaster

3643a1a65fc88ae0e9f28f12168629758d027415

.gitlogsrefsheadsmaster

0000000000000000000000000000000000000000 
		3643a1a65fc88ae0e9f28f12168629758d027415 
		......	
		commit (initial): first commit

.gitlogsrefsremotesoriginmaster

0000000000000000000000000000000000000000 
		3643a1a65fc88ae0e9f28f12168629758d027415 
		......	
		update by push

after:

.gitrefsheadsmaster

64df093f73294d82a3adce9694871b9fac2aecfb

.gitrefsremotesoriginmaster(不变)

.gitlogsrefsheadsmaster

0000000000000000000000000000000000000000 
		3643a1a65fc88ae0e9f28f12168629758d027415 
		......	
		commit (initial): first commit
3643a1a65fc88ae0e9f28f12168629758d027415 
		64df093f73294d82a3adce9694871b9fac2aecfb 
		......	
		pull origin master:master: fast-forward

.gitlogsrefsremotesoriginmaster(不变)

本地库更新至最新,git pull会将本地库更新至远程库的最新状态

由于本地库进行了更新,HEAD也会相应的指向最新的commit id

所以虽然从结果上来看,git pull = git fetch + git merge,但是从文件中保存的commit id来看,实现上不是这样实现的

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

 

版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/a19881029/article/details/42245955
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2021-04-10 20:19:07
  • 阅读 ( 612 )
  • 分类:

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢