The difference between the Git pull command and the fetch command

  • 2020-05-24 06:42:51
  • OfStack

The difference between the Git pull command and the fetch command

Today, I met a problem in the company. The company does not use master branch as the main branch, but release branch as the main branch, which caused a problem. In other words, when the project of clone1 comes to an end, if master has a conflict with release branch, pull will not be able to solve the conflict by itself.

As soon as I get to the next project, I don't know what the conflict is about, which makes it very difficult.

At this point, we have to mention the two commands of Git, git fetch and git pull

In Git, there are two commands to get the latest version from the remote branch to the local:

Git fetch

git fetch: this is equivalent to getting the latest version locally from remote, not automatically merge


git fetch origin master
git log -p master..origin/master
git merge origin/master

Meaning of the above command:

First download the latest version from the remote origin master main branch to the origin/master branch

Then compare the local master branch with the origin/master branch

And then finally merge

The above process can be done in a clearer way:


#  The remote release Branch to local releaselocal branch 
git fetch origin releaser:releaselocal

#  Switch to the tmp branch 
git checkout releaselocal

The master branch is then deleted and the master branch is created directly from releaselocal, so that all branches are synchronized with the remote release branch.

Git pull

git pull: equivalent to getting the latest version from remote and merge to local


git pull origin release:release

The above command is actually equivalent to git fetch and git merge

If there is no local release branch, it will automatically follow the current branch1 release branch and then perform the pull operation

So what I usually do is keep the local master branch without doing any commit, and then switch through the master branch so that it is clean on the master branch, and then pass the previous command without causing any conflicts

The merge operation is then performed on the release branch, which is much safer.

Thank you for reading, I hope to help you, thank you for your support of this site!


Related articles: