Make Selenium Easy

Git Tutorial 10 – Git Fetch – Download (Not Merging) Changes From Remote Repository

Introduction

As a part of GIT Tutorials End To End, we will learn about another frequently used and important git command “git fetch“ in this post.

Prerequisite posts

I will expect that you are aware of the basic concepts and commands of GIT. But if you are a beginner in GIT then I strongly recommend you to refer GIT Basic Commands and Concepts section on my blog first.

Did you know that I have started a YouTube channel as well and I need your support to make it successful. Please do watch content then comment, like, share, and obviously subscribe.

What is Git Fetch command?

In real-time more than one developer will be working on the same repository. Suppose there are two developers in a project – DevA and DevB who have the same code base. DevA developed a feature in his local repository and push those new codes to the remote repository. DevB wants those changes in his local repository.

DevB does not want DevA changes to directly impact his local repository instead he wants to see or review those changes. Here DevB needs to use the “git fetch” command.

The “git fetch” command downloads commits, files, and refs from a remote repository into your local repo but not in your currently checked-out branch. It helps to isolate changes fetched from the remote repository and local changes. In simple words, no merging of code will happen. For example – If DevA has added a file “DevA File” in the remote repository and DevB has also added a file “DevB File” in his local repository then the git fetch command will not add “DevA File” in DevB local repository.

I will demonstrate the git fetch command with an example now.

We will use a public demo repository from my GitHub.

Repository link – https://github.com/amod-mahajan/gitFetchExample.git

This repo has one commit and one file.

Steps to perform to understand git fetch command

  1. Clone the above repository in your local. I have already covered “git clone” command in detail here.

2. Run “git log” command to view existing commits. We should also check existing files. You can go to the folder (gitFtechLocalRepo in my case) in the file directory as well to check the content.

3. Run “git branch -a” command to see all branches after cloning.

The output of the above requires a detailed explanation which I will cover later. In short “main” is a currently checked-out local branch. In GitHub default branch name is “main” which is similar to “master” in other Git hosting service providers. When we clone a repo we see two other branches as well. The branch “remotes/origin/main” is a local copy of the main branch of the remote repository. The branch “remotes/origin/HEAD” represents the default branch on the remote which is “main”.

4. Now we have cloned the repo in our local. Let’s add a new file in the repo directly using GitHub. Follow the steps shown below to add and commit a new file. It is similar to the scenario above in which DevA pushes a new feature to the remote repository.

5. This newly added file will not be available to my locally cloned repository. Now run the “git fetch origin main” command. This command says that fetch the content from the “main” branch in the “origin” repo.

6. You can see that the new file is not showing while running ls command which is expected as the git fetch command does not impact the current state of the local checkout branch. Now you might be thinking where are imported changes then. That is actually in “remotes/origin/main” branch which we saw in step no 3 above. This branch keeps a copy of the “main branch” of the remote repository. When we do “git fetch” command then this branch is actually updated. Please note this branch will not have any changes made in your local branch. let’s run another git command “git checkout“. We will navigate to the branch where changes were fetched to verify if changes were fetched.

You can see in the above image that we have checked out to origin/main branch and whose HEAD is at new commit in which we added “File B”. We can see the newly added file with ls command.

7. Now if we want to merge or bring remote changes to our local branch then we need to use “git merge” command. The command “git merge origin/main” says that merge changes from “origin/main” branch to my currently checked out branch which is “main“.

Please subscribe to my YouTube channel Retarget Common to learn from my video tutorials.

Below are important end to end tutorials for Testers:-

Selenium Tutorials

Rest Assured Tutorials

Postman Tutorials

TestNG Tutorials

If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe.
#ThanksForReading
#HappyLearning

Author: Amod Mahajan

A software Tester who is paid to judge products developed by others. Currently getting paid in American Dollars. Writing technical posts and creating YouTube videos are my hobbies.

Leave a Reply

Please wait...

Subscribe to new posts to become automation expert

Want to be notified when my new post is published? Get my posts in your inbox.

Git Tutorial 10 – Git Fetch – Download (Not Merging) Changes From Remote Repository

by Amod Mahajan time to read: 3 min
0
%d bloggers like this: