Signup/Sign In

Git Fetch

We know the importance of collaborating with other developers on a project. Git Fetch is another command that lets us interact with the remote repository and download the changes made to the remote repository by other team members. However, it does not alter our local repository and keep local commits intact. Let's understand the working of Git Fetch in this tutorial.

What is Git Fetch?

  • Git Fetch is a command that lets us download the changes from a remote repository and to our local repository but stores them as a separate branch which is referenced by using the remote-tracking branches. It keeps the local master branch unchanged.
  • Fetching is just a way of looking at what others have done in the remote repository without altering our own work.
  • Git Fetch is similar to the Git Pull command as both of them download the new updates from the remote repository but unlike Git Pull, Git Fetch keeps these changes as a separate branch and does not perform any merge on the local master and the remote-tracking branches.

How Git Fetch Works?

The job of the Git Fetch command is to retrieve and store the changes that were made in the remote repository. It does so with the help of remote-tracking branches. These branches tell Git the exact point where the two repositories(local and remote) have changed from and so Git knows the exact commit point from where it should start fetching from the remote repository.

Let's try to understand the working of Git Fetch with the following illustration:

  1. Let's say that we cloned a remote repository that had three commit points A, B, and C. After cloning we will have a remote-tracking branch reference(origin/master) at the C, and the local master branch reference will also point to C.After cloning both local master and the remote tracking branch point to the same commit.
  2. Now consider that a developer added two commits to the remote repository and you added a single commit to your local repository. Now the local master branch has a commit point D and the remote master branch has the commits E and F. After adding the two new commits to the remote repository and one new commit to the local repository.
  3. If we execute the Git Fetch command then Git fetches the two new commits E and F and adds them to the remote-tracking branch.After running git fetch the two new commits of the remote repository are added to the remote tracking branch.
  4. The local master branch will still have the same four commits A, B, C, and D but now we can see the two new commits E and F in a separate branch.

Git Fetch Command

The Git Fetch command can be used to fetch remote repository data in a few different ways. Let's look at the command and the different options we can use with it.

To fetch the entire remote repository:

$ git fetch <remote-repository-url>

To fetch just a single branch from the remote repository:

$ git fetch <remote-repo-url> <branch>

To fetch all the branches from the remote repository, we can use the --all flag. It will fetch the changes for all the remote-tracking branches.

$ git fetch --all

Summary

Git Fetch is a very commonly used command to communicate with the remote repository. Git Fetch only downloads the new commits in a separate branch without merging them with the branch on which we are currently working. We learned how remote-tracking branches help Git in fetching the new changes from the remote repository. We can use Git Fetch for just fetching a single branch or we can even fetch all the branches from the remote repository. In the next article, we will see the differences between Git Pull and Git Fetch commands.



About the author:
I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development. Founder @ Studytonight