The difference between git fetch and git pull


Git fetch and git pull can update the remote repository to local. What’s the difference between them? There are several concepts that have to be mentioned to clarify this issue.

FETCH_HEAD:Is a version link, recorded in a local file, pointing to the end version of the branch that has been removed from the remote repository.  

commit-id:After each local work is completed, a git commit operation is performed to save the current work to the local repo. At this time, a commit ID is generated, which is a serial number that can uniquely identify a version. After using git push, the serial number will be synchronized to the remote repository.

With the above concepts, let’s talk about git fetch

git fetch:This will update the latest commit IDs of all branches contained in the remote repository in Git remote and record them to. Git / fetch_ In the head file
Git fetch updates the remote warehouse in the following way:

git fetch origin master:tmp 
//Create a new local temp branch and download the master branch code of remote origin warehouse to the local temp branch
git diff tmp 
//To compare the difference between the native code and the code just downloaded from a remote location
git merge tmp
//Merge temp branch to local master branch
git branch -d temp
//If you don't want to keep the temp branch, you can use this step to delete it

(1) If git fetch is used directly, the steps are as follows:

  • Create and update local remote branches. That is to create and update the origin / xxx branch and pull the code to the origin / xxx branch.
  • In fetch_ Set the current branch origin / current branch correspondence in head. For example, the origin / ABC can be merged into the ABC branch by git merge directly.

(2)git fetch origin
You just manually specified the remote to fetch. When no branch is specified, the default is master

(3)git fetch origin dev 
Specify remote and fetch_ Head, and only pull the commit of this branch.

This command will access the remote repository and pull all the data you don’t have. After execution, you will have references to all branches in that remote repository, which can be merged or viewed at any time.

If you clone a warehouse with git clone command, the command will automatically add it as git remote – V and use “origin” as the abbreviation by default. So git fetch origin grabs all the work that has been pushed since the clone (or the last fetch). It’s important to note that the GIT fetch command pulls data to your local repository – it doesn’t automatically merge or modify your current work. When ready, you have to merge it manually into your work.

If you have a branch set to track a remote branch, you can use the GIT pull command to automatically grab and then merge the remote branches to the current branch. This may be a simpler or more comfortable workflow for you; by default, the GIT clone command automatically sets the local master branch to track the master branch of the cloned remote repository (or whatever the name of the default branch). Running git pull usually grabs data from the server that was originally cloned and automatically attempts to merge into the current branch.

git pull : First, based on local fetch_ Head record, compare with local fetch_ The head records the version number of the remote warehouse, and then git fetch gets the data of the subsequent version of the remote branch currently pointed to, and then merges it with the local current branch by using git merge. So git pull is a combination of GIT fetch and git merge.

Git pull is used as follows:

Git pull < remote host name > < remote branch name >: < local branch name >
//Retrieve the update of a branch of the remote host and merge it with the local specified branch.

Therefore, compared with git pull, GIT fetch gets the latest version from remote to local, but does not automatically merge. If there is a need for selective merging, GIT fetch is a better choice. At the same time, GIT pull will be faster.

Note: using git pull to update the code is relatively simple and violent. However, according to the commit ID, their actual implementation principles are different, so do not use git pull, and git fetch and git merge are more secure.

This article about the difference between git fetch and git pull will be introduced here. For more information about the differences between git fetch and git pull, please search the previous articles of developeppaer or continue to browse the related articles below. I hope you can support developeppaer more in the future!

Recommended Today

JS multi image upload, get a number of images Base64 value display to the page

Display effect before uploadingDisplay effect after uploading $(‘#img_purchase’).on(‘click’,function(){ $(‘#purchase’).click(); }); $(‘#img_sale’).on(‘click’,function(){ $(‘#sale’).click(); }); function getBase64 (file,Callback) { let reader = new FileReader() let imgResult = ” reader.readAsDataURL(file) reader.onload = function () { imgResult = reader.result } reader.onerror = function (error) { Callback(error) } reader.onloadend = function () { Callback(imgResult) } }; $(document).on(‘change’, ‘#purchase’, function () […]