Difference Between Git Fetch and Git Pull

John Wachira Mar 25, 2022
  1. What is Git Fetch
  2. What is Git Pull
  3. Difference Between Git Fetch and Git Pull
Difference Between Git Fetch and Git Pull

This article will discuss the practical uses of the git pull and git fetch commands to see how they differ and when to use them.

What is Git Fetch

In our remote repository, we have the files shown below.

Remote repository

Mind you, our remote and local repositories are in sync. Let’s change the Sample.txt file in the remote repository.

Write something random in the file and commit the changes.

updated file

At this point, our remote repository has two commits, while our local repository has one commit. This means our local repository needs an update.

git fetch is handy at this point. Let’s run a git log command to check our commit history in our local repository.

Commit history

Our local repository does not have the Updated Sample.txt commit. We use git fetch to check for changes.

pc@JOHN MINGW64 ~/Git/Delftscopetech (main)
$ git fetch
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), 709 bytes | 2.00 KiB/s, done.
From https://github.com/Wachira11ke/Delftscopetech
   c43169e..2445daf  main       -> origin/main

From the command above, we can see a change in our remote repository made to the main branch. We can apply these changes to our local repository with the git merge command.

pc@JOHN MINGW64 ~/Git/Delftscopetech (main)
$ git merge origin main
Updating c43169e..2445daf
Fast-forward
 Sample.txt | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Let us run the git log command to check our commit history.

$ git log
commit 2445dafc118748a5cb53c5262b393ab0e1a9e235 (HEAD -> main, origin/main, origin/HEAD)
Author: Wachira11ke <100116527+Wachira11ke@users.noreply.github.com>
Date:   Tue Mar 15 22:07:11 2022 +0300
    Update Sample.txt
    First Update
commit c43169e587ab7ab996087ff460e54032e83030f0
Author: Wachira11ke <100116527+Wachira11ke@users.noreply.github.com>
Date:   Tue Mar 15 21:45:50 2022 +0300
    Second commit
commit b2f77108396c9ae867d8e9d69c575eda99dd1436
Author: Wachira11ke <100116527+Wachira11ke@users.noreply.github.com>
Date:   Mon Feb 21 10:00:23 2022 +0300
    Initial commit

At this point, both our local and remote repositories are in sync. Let us look at the git pull command.

What is Git Pull

Let us go back to our remote repository and make more changes to our file. Write a random figure and commit the changes.

Now, our remote repository has one commit more than our local repository. We can run the git pull command to update our local repository.

pc@JOHN MINGW64 ~/Git/Delftscopetech (main)
$ git pull origin main
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), 730 bytes | 3.00 KiB/s, done.
From https://github.com/Wachira11ke/Delftscopetech
 * branch            main       -> FETCH_HEAD
   2445daf..8089f2b  main       -> origin/main
Updating 2445daf..8089f2b
Fast-forward
 Sample.txt | 1 +
 1 file changed, 1 insertion(+)

Our local repository is now up to date with our remote repository. We can conclude that git pull is a compound command (git fetch + git merge).

It fetches and merges the changes directly.

Difference Between Git Fetch and Git Pull

Git fetch Git pull
Checks for changes made to the remote repository. Merge changes from the remote repository with the local repository directly.
The fetched changes are updated to a .git folder. The changes are made to your local repository directly.
You can review the commits before merging. You will update changes instantly.
Conflicts rarely appear. Merge conflicts are likely to occur.
Author: John Wachira
John Wachira avatar John Wachira avatar

John is a Git and PowerShell geek. He uses his expertise in the version control system to help businesses manage their source code. According to him, Shell scripting is the number one choice for automating the management of systems.

LinkedIn

Related Article - Git Pull

Related Article - Git Fetch