Difference between git fetch and git pull

When working with Git, we will often use the “git fetch” and “git pull” commands to update data from the Git remote repository to the Git local repository. So what’s the difference between these two commands is, let’s find out in this tutorial!

First, I’ll talk about “git fetch” first.

The command that we usually use with “git fetch” is “git fetch origin”. Using this command, you can update the latest information from Git remote repository. But the information here does not mean that the entire data from the Git remote repository, it’s just the metadata information. For example, which branches our Git remote repository has, there are any changes from the Git remote repository that we have not updated yet? It only updates that information, not making changes, synchronizing Git local repository with Git remote repository.

What about “git pull”?

We often use the “git pull origin <branch_name>” command to update source code changes in the branch we are working on. It will update all those changes to the Git local repository, not like the “git fetch” command, only update the metadata, not update the source code.

Because of that, using “git pull”, we often encounter a problem called source code conflict. The reason is because the code we changed in the Git local repository, someone also changed on the Git remote repository, updated that code, Git does not know which code to choose to merge.

For everyone to see this difference, I will do an example as follows:

I have a repository named “test” on Bitbucket as follows.

Difference between git fetch and git pull

On my machine:

Difference between git fetch and git pull

Now suppose I create a new test-git branch on Bitbucket:

Difference between git fetch and git pull

Then use the command “git pull origin master”, then “git checkout test-git” to switch to the test-git branch that I just created above, you will see the error as follows:

Difference between git fetch and git pull

However, if you use the command “git fetch origin” and “git checkout test-git”, you will see the results as follows:

Difference between git fetch and git pull

Difference between git fetch and git pull
Đánh giá bài viết
Chia sẽ bài viết này ...Share on Facebook
Facebook
3Tweet about this on Twitter
Twitter
Share on LinkedIn
Linkedin

Add Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.