Home  >  Article  >  System Tutorial  >  Learn more about the difference between git fetch and git pull

Learn more about the difference between git fetch and git pull

王林
王林forward
2024-04-27 21:34:33432browse

详细了解git fetch与git pull的区别

Both git fetch and git pull can update the remote repository to the local one. So what are the differences between them? There are several concepts that have to be mentioned to clarify this issue.

FETCH_HEAD: It is a version link, recorded in a local file, pointing to the end version of the branch that has been taken down from the remote warehouse.
commit-id: After each local work is completed, a git commit operation will be performed to save the current work to the local repo. At this time, a commit-id will be generated, which can uniquely identify a version. serial number. After using git push, this serial number will also be synchronized to the remote warehouse.

With the above concepts, let’s talk about git fetch
git fetch: This will update the latest commit-id of the branches contained in all remote repositories in git remote and record it in the .git/FETCH_HEAD file
The way git fetch updates the remote warehouse is as follows:

git fetch origin master:tmp <span class="hljs-comment">
//在本地新建一个temp分支,并将远程origin仓库的master分支代码下载到本地temp分支</span>
git diff tmp <span class="hljs-comment">
//来比较本地代码与刚刚从远程下载下来的代码的区别</span>
git <span class="hljs-built_in">merge</span> tmp<span class="hljs-comment">
//合并temp分支到本地的master分支</span>
git branch -d temp<span class="hljs-comment">
//如果不想保留temp分支 可以用这步删除</span>

(1) If you use git fetch directly, the steps are as follows:

  • Create and update local remote branches. That is, create and update the origin/xxx branch, and pull the code to the origin/xxx branch.
  • Set the current branch-origin/current branch correspondence in FETCH_HEAD. If you directly git merge at that time, origin/abc can be merged into the abc branch.

(2)git fetch origin
Just manually specify the remote to fetch. When no branch is specified, it usually defaults to master

(3)git fetch origin dev
Specify remote and FETCH_HEAD, and only pull the commits of this branch.

git pull: First, based on the local FETCH_HEAD record, compare the local FETCH_HEAD record with the version number of the remote warehouse, then git fetch obtains the subsequent version data of the remote branch currently pointed to, and then Use git merge to merge it with the current local branch. So it can be considered that git pull is a combination of the two steps of git fetch and git merge.
The usage of git pull is as follows:

git pull <span class="hljs-tag">60c690392010ae04a4c92ce6c44e5cd2</span> <span class="hljs-tag">2e81c6e41cd53cc8483c7ee2820f9055</span>:<span class="hljs-tag">a70f6d52bffd2c4d7fbfb25461f834a0</span>
//取回远程主机某个分支的更新,再与本地的指定分支合并。

Therefore, compared with git pull, git fetch is equivalent to getting the latest version from the remote to the local, but it will not automatically merge. If you need to selectively merge git fetch is a better choice. When the effect is the same, git pull will be faster.

The above is the detailed content of Learn more about the difference between git fetch and git pull. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:linuxprobe.com. If there is any infringement, please contact admin@php.cn delete