git fetch workflow explained [With Easy Examples]

 

Understanding the fetch command in git

  • The git fetch command facilitates the transfer of remote branch refs, files, tags and commits into your local projects.
  • Git fetch helps to keep your local repository updated with the remote changes from a collaborated project.
  • The fetch operator process is different from git pull although very similar in behaviour. It facilitates the download of remote changes without merging them into a corresponding local repository. Also, it’s considered to be a more safe approach as the downloaded content do not interfere with your current local developments.
  • Compared to git pull,  the pull function is aggressive as it will update the content into your local project. Git pull automatically merges the received content into the local repository.
  • Git checkout command works closely with the fetch function. You will use git checkout to access the fetched data from the remote project. Using the fetch command to obtain remote updates helps to avoid issues resulting from merge conflicts. The fetching operator creates remote-tracking branches that you can later push upstream after merging into local commits.
  • Fetching serves to keep check of all the remote updates that have taken place in the remotes project whereas git pull ensures that all changes are merged into the local repository.

In this tutorial about git fetch command we shall explain the git fetch concept, the workflow and demonstrate using examples the different git fetch scenarios.

 

Git fetch syntax

Following is the syntax to use git fetch command. You can refer official documentation to get the list of all supported options:

Advertisement
git fetch [<options>] [<repository> [<refspec>…​]]
git fetch [<options>] <group>
git fetch --multiple [<options>] [(<repository> | <group>)…​]
git fetch --all [<options>]

 

Git fetch workflow

From the diagram, the fetch command transfers the latest changes to your active repo and not directly to your remote project. It provides a window for implementing any other changes before you can merge them into your local project.

git fetch

 

Here is one more diagram which explains the workflow for git fetch.

  • Figure a represent before fetch operation
  • Figure b represent after fetch operation where all the refs, tags and commits have been pulled from the remote branch but the master is still pointing to the last commit before fetch operation
  • Figure c represent after merge operation where the master is pointing to the latest commit

git fetch workflow explained [With Easy Examples]

Setting the lab environment

The fetch function operates between the remote repository and your local project via remote-tracking branches. To demonstrate how that works, we will first clone the remote project git-tester. That will enable you to have the project locally for practice.

I will clone git-tester on my local workstation windows 10 pro.  Also, I will be using git version 2.32.0.windows.2 for this git fetch experiment.

$ git clone https://github.com/J-Mulombi/git-tester.git
Cloning into 'git-tester'...
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Receiving objects: 100% (3/3), done.

Now that we have a local project set, we will move to the next section of the tutorial to practice using the fetch operator in git with examples.

 

Fetch command working  examples

Example-1: Download a remote repository using the fetch function

You can transfer a remote repository and its history to your local working repository using the fetch function. This operation is similar to running a git pull command. However, with git fetch, the downloaded contents will not be automatically merged into your local project.

Advertisement

Assuming that git-tester is a collaborated project and a team member has just made some changes. We are going to use git fetch to have those changes to the local server as follows;

$ git fetch https://github.com/J-Mulombi/git-tester.git
remote: Enumerating objects: 6, done.
remote: Counting objects: 100% (6/6), done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 5 (delta 1), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (5/5), 1.23 KiB | 40.00 KiB/s, done.
From https://github.com/J-Mulombi/git-tester
 * branch            HEAD       -> FETCH_HEAD

We have fetched the latest updates from the remote project git-tester.

To view the commits that were done upstream in the above illustration, we will run git fetch -v command as shown below;

$ git fetch -v
POST git-upload-pack (154 bytes)
POST git-upload-pack (268 bytes)
remote: Enumerating objects: 10, done.
remote: Counting objects: 100% (10/10), done.
remote: Compressing objects: 100% (8/8), done.
remote: Total 9 (delta 2), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (9/9), 2.41 KiB | 94.00 KiB/s, done.
From https://github.com/J-Mulombi/git-tester
   e5c2644..2e030f0  master     -> origin/master
 * [new branch]      experiment -> origin/experiment

From the results, there’s the master branch with commits and a new remote branch called experiment.

 

Example-2: Download a specific remote branch using fetch function

Let’s say you want to use commits from a particular remote branch within the collaborated project. What will you do? Using the fetch function you can download that remote branch by running the following command:

$ git fetch <branch URL> <branch name>

Let's use an example to illustrate further. We will fetch a remote branch experiment from the remote repository git-tester as shown below;

$ git fetch https://github.com/J-Mulombi/git-tester.git experiment
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 2 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (2/2), 663 bytes | 34.00 KiB/s, done.
From https://github.com/J-Mulombi/git-tester
 * branch            experiment -> FETCH_HEAD

The results show that we have fetched remote branch experiment into our local repository.

Git works to ensure that there's no mix-up in the remote content and the local one although they are a copy of each other. Git uses refs to distinguish between the local and remote branches of a project. To view all branches both local and remote use the git branch -a command.

Advertisement

In case you want to switch to a specific branch git employs the use of the git checkout command.

To view the fetched commits in the experiment branch, we shall run git fetch –v command as follows:

$ git fetch -v
POST git-upload-pack (154 bytes)
From https://github.com/Maureen-Mulombi/git-tester
 = [up to date]      master     -> origin/master
   ddfd27f..3bece02  experiment -> origin/experiment

 

Example-3: Download all remote branches using the fetch operator

You can download all remote branches at once using the fetch operator as follows;

$ git fetch --all
Fetching origin
From https://github.com/J-Mulombi/git-tester
 * [new branch]      latest-branch -> origin/latest-branch
 * [new branch]      tools-branch  -> origin/tools-branch

Next, run the git fetch –v command to view all the fetched commits as shown below:

$ git fetch -v
POST git-upload-pack (154 bytes)
From https://github.com/Maureen-Mulombi/git-tester
 = [up to date]      master        -> origin/master
 = [up to date]      experiment    -> origin/experiment
 = [up to date]      latest-branch -> origin/latest-branch
 = [up to date]      tools-branch  -> origin/tools-branch

 

Example-4: Use git fetch to synchronize all remote updates to the local repo

You can synchronize the latest updates from other collaborators to your local repository using git fetch origin command as follows;

$ git fetch origin
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 2 (delta 1), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (2/2), 625 bytes | 29.00 KiB/s, done.
From https://github.com/J-Mulombi/git-tester
   2e030f0..5046c88  master     -> origin/master

The output shows the latest commits performed in the origin/master branch.

 

Example-5: Prune stale commits using the fetch function in git

You can use git fetch --prune command to do away with the stale commits in your local repository. Let's use an example to illustrate how this works.

In this case, a team member deleted a file named feat.txt from the latest-branch as shown:

git -fetch-prune

To view the commit of the deleted file and to remove it so that it no longer takes up the working space we shall use git fetch --prune command as follows:

$ git fetch -p
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 2 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (2/2), 649 bytes | 81.00 KiB/s, done.
From https://github.com/Maureen-Mulombi/git-tester
   3bece02..84a2fb3  latest-branch -> origin/latest-branch

The output shows the recent commit 84a2fb3 that was deleted from the remote branch latest-branch. Pruning in git helps to do away with wasted commits that are no longer in use. Running the git fetch –prune commands helps to identify such commits.

 

Example-6: Download tags using git fetch

You can download tags from the remote repository using git fetch tag --all --tags command as follows;

$ git fetch --all --tags
Fetching origin
From https://github.com/J-Mulombi/git-tester
 * [new tag]         v2.0       -> v2.0

The output shows that the remote repository git-tester has a new tag v2.0.

 

Summary

We have extensively covered the following topics in this tutorial about fetch function:

  • Understanding the fetch function
  • Fetch function workflow
  • Working examples for the fetch operator

 

Further Reading

git-fetch, git-pull

 

Didn't find what you were looking for? Perform a quick search across GoLinuxCloud

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can either use the comments section or contact me form.

Thank You for your support!!

Leave a Comment