Open In App

Difference Between Git Fetch and Git Pull

Improve
Improve
Like Article
Like
Save
Share
Report

Git Fetch command fetches all the changes from the remote repository to the local repository without bringing the changes into the working directory. Git Pull on the other hand brings the copy of the remote directory changes into the working directory. In this article, Let us look at Git Fetch and Git Pull separately with the help of an example.

What is Git Fetch?

Git Fetch command is used to fetch all changes from the remote repository to the local repository. It doesn’t make any changes to the current working directory. It stores all the changes in a separate branch called the remote-tracking branch. git merge or git rebase command is used to merge these changes into our current working directory.

How to Use Git Fetch?

Step 1: Let us create a file called demo.txt with “Hello Geeks” content inside it initialize the directory to a git repository and push the changes to a remote repository.

  • git init
  • git add <Filename>
  • git commit -m <Commit Message>
  • git remote add origin <Link to your remote repository>
  • git push origin <branch name>

 

Now, we have my demo.txt in the remote repository. 

 

The local and the remote repositories are now in sync and have the same content at both places. Let’s now update our demo.txt in the remote repository

Step 2: We will update our demo.txt remotely.

 

Now since we have updated our demo.txt remotely, let’s bring the changes to our local repository. Our local repository has only 1 commit while the remote repository now has 2 commits (observe the second commit starting from 4c4fcb8).

Step 3: Let’s use the git fetch command to see in the local repository whether we have a change in the remote repository or not. Before that let’s use the git log command to see our previous commits.

 

We can see that after using git fetch we get the information that there is some commit done in the remote repository. (notice the 4c4fcb8 which is the initials of our 2nd commit in a remote repository).

Step 4: To merge these changes into our local repository, we need to use the git merge origin/<branch name> command. 

 

Let us have a look at our commits in the local repository using the git log command.

 

And we got our remote repository commit in our local repository. This is how git fetch works. Let us now have a look at the git pull command.

What is Git Pull?

Git Pull command is used to fetch all changes from the remote repository to the current working directory. It automatically try to merge or rebase them into our current working directory. It is the combination of git fetch and git merge or git rebase. It can generate merge conflicts if there are conflict changes between our local and remote branches.

How to Use Git Pull?

Step 1: Let’s make more changes to our demo.txt file at the remote repository.

 

Now, we have 3 commits at our remote repository whereas 2 commits at our local repository. (Notice the third commit starting with 09d828f).

Step 2: Let us now bring this change to our local repository using the git pull origin <branch name> command.

 

We can see that with the help of just git pull command, we directly fetched and merged our remote repository with the local repository.

git pull = git fetch + git merge

Let us see what our demo.txt in the local repository looks like – 

 

And now our remote and local repositories are again in sync with each other.

Difference between Git Fetch and Git Pull

Git Fetch 

Git Pull

Used to fetch all changes from the remote repository to the local repository without merging into the current working directory Brings the copy of all the changes from a remote repository and merges them into the current working directory
Repository data is updated in the .git directory The working directory is updated directly
Review of commits and changes can be done Updates the changes to the local repository immediately.
No possibility of merge conflicts. Merge conflicts are possible if the remote and the local repositories have done changes at the same place.
Command for Git fetch is git fetch<remote> Command for Git Pull is git pull<remote><branch>
Git fetch basically imports the commits to local branches so as to keep up-to-date that what everybody is working on. Git Pull basically brings the local branch up-to-date with the remote copy that will also updates the other remote tracking branches.

Last Updated : 01 Nov, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads