Tutorial 7 : git pull and git fetch

Missed previous tutorial ? — click here.

Understand following terminologies first, we will demystify them one by one in this tutorial:

  • git pull — downloads content from remote repository
  • git pull = git fetch+ git merge
  • git fetch — copies remote repository in local repository
  • git merge — brings the content from local repository to working directory

Above image will make more sense if you know difference between working directory and local repository from first tutorial.

This tutorial is divided in following parts :

Set-up

Part 1 : git fetch + git merge

Part 1 : git pull

Part 2: git fetch + git merge

Part 2: git pull

Set-up :

First create a new repository. I created repository in my github account by the name: git-fetch-pull

We will try to prove the equation :

git pull = git fetch+ git merge

Add one text file in this repository. In my repository file1.txt was created in master branch.

So my repository looked like this :

Part 1 : git fetch + git merge

First we will check the results of combined actions of fetch and merge. We can check the results in one folder.

So create an empty folder in PC desktop. I created folder by the name: fetch_folder.

Open terminal in that folder and run following commands in command prompt.

Initialize an empty repository :

git init

Since we are copying content from remote repository we created above, we need to add remote :

git remote add origin https://github.com/gaurav1patil/git-fetch-pull.git

Now we want the contents to be fetched from this URL .

git fetch origin

As we know :

git fetch — copies remote repository in local repository

Let’s bring this content from local repository to working directory i.e. our folder fetch_folder in PC by merging :

git branch -agit merge origin/main

While merging we need to specify the branch we want. In our case we wrote “origin/main” since it is main branch of the remote repository.

As we know :

git merge — brings the content from local repository to working directory

Check the fetch_folder in PC, you will find the file1.txt file from remote repository in this folder.

Result : File from master branch of remote repository found in the PC folder.

Part 1 : git pull

Let’s try to replicate the same results by using git pull command

Create an empty folder in PC desktop I created folder by the name: pull_folder.

Open terminal in pull_folder and run following commands in command prompt.

Initialize an empty repository :

git init

Since we are copying content from remote repository, we need to add remote :

git remote add origin https://github.com/gaurav1patil/git-fetch-pull.git

Now we want the contents to be pulled from this URL .

git pull origin main

As we know:

git pull — downloads content from remote repository

Check the pull_folder in PC, you will find the file1.txt file from remote repository in this folder.

Result : File from master branch of remote repository found in the PC folder.

Since we got same result, it is proved that :

git pull = git fetch+ git merge

Let’s see if it works if we have multiple branches as done below.

Part 2 : git fetch + git merge

In the same github repository, create another file in another branch. In my repository I created file2.txt and added in branch2 branch.

Now let’s follow the same process while being in fetch_folder in command prompt (where previous fetch commands were running) :

git fetch origingit branch -agit merge origin/branch2

Check the fetch_folder in PC, you will find the file1.txt and file2.txt files from remote repository in this folder.

Part 2 : git pull

Now let’s follow the same process while being in pull_folder in command prompt (where previous pull commands were running):

git pull origin branch2

Check the pull_folder in PC, you will find the file1.txt and file2.txt files from remote repository in this folder.

Hence again it is proved that :

git pull = git fetch+ git merge

Congratulations !!!

Now that you have completed seven tutorials, you have very good understanding of how different functionalities in git work.

Thank you !!!

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Gaurav Patil

Gaurav Patil

115 Followers

Machine Learning Engineer drawing insights from mathematics.