Git Fetch



The git fetch command download commits, files, and refs from a remote repository into the local repository. Fetching is used to see what other developers have been working on.

Git separate fetched content from existing local content. Fetching does not affect local development work.

Fetching is a safe way to examine commits before combining them with the local repository. The fetched content is not directly integrated with the local repository. It should be explicitly checked out using the git checkout.


How git fetch works with remote branches

Before going deep into the git fetch command, let us understand how Git manages and stores commits. Git saves all commits, local and remote, in the repository's ./.git/objects directory. Git stores the local commits separated from the remote commits through the use of branch references. The references for the local branches are stored in the ./git/refs/heads/. To see the list of the local branch references, run the git branch as follows:

$ git branch 
master 
develop 
feature_branch

The same above output will be seeing if we examine the /.git/refs/heads/ directory content.

$ ls ./.git/refs/heads:
master 
develop 
feature_branch

Remote branches are prefixed by the remote they relate to, so they do not mix up with local branches. Remote branches are stored in ./.git/refs/remotes directory. The example bellow list the branches we might see after fetching a remote repo name 'remote_repository':

$ git branch -r
origin/master
origin/develop
origin/feature_branch
remote_repository/master 
remote_repository/feature_authentication

The output above shows the remote branches prefixed with origin/. Also, it lists the remote branches prefixed with remote-repo/. It is possible to check out a remote branch just like a local, but it puts in a detached HEAD state.

To show remote branches, use the -r flag with the git branch command.

You can verify the remote branches with the git checkout and git log commands. After accepting the changes of the remote branch, you can merge (integrate) it into the local branch using the git merge command. To save time you can use git pull command to shortcut the process.


Git Fetch Commands and Options

The git fetch command has several options. The following list shows different use cases.

  • <remote> : It will fetch all branches from the repository. It will also download all commits and files.

    $ git fetch <remote>
    
  • <remote> <branch>: It is the same as above. It will download all commits and files but only for the specified .

    $ git fetch <remote> <branch>
    
  • --all: It will fetch all registered remotes and their branches.

    $ git fetch --all
    
  • --dry-run: It will show the command's demo running without making any changes.

    $ git fetch --dry-run
    
  • -p or --prune: It will remove any remote-tracking that no longer exists on the remote before fetching.

    $ git fetch --prune
    

How to git fetch the remote branch

The example below shows how to fetch a remote branch and update the local working state to the remote content. We have a central repository origin from where the local repository has been cloned using the git clone command. Another remote repository named demo_repo contains a feature_demo_branch that we will configure and fetch.

First step is the add the remote repository with git remote command.

$ git remote add demo_repo git@hostname:username/demo_repo .git

In the above command, we have created a new reference to the demo_repo repository.

Now we will use the remote with the git fetch command to download contents as follows:

$ git fetch demo_repo feature_demo_branch
fetching demo_repo/feature_demo_branch 

We downloaded the "demo_repo/feature_demo_branch" contents and will need to integrate them into our local working directory.

To integrate the downloaded contents into the local working directory, we can use the git checkout command to checkout the downloaded remote branch.

git checkout demo_repo/feature_demo_branch 
Note: checking out demo_repo/feature_demo_branch . 

You are in 'detached HEAD' state. You can look around, make experimental changes and commit them, and you can drop all the commits you make in this state without influencing branches by executing another checkout.

 If you want to generate a new branch for maintaining commits you create, you may do so (now or later) if you use -b with the checkout command again. 

Example: git checkout -b <new-branch-name>

The above output tells that we are in a detached HEAD state, which means that the HEAD reference points to a reference that is not in sequence with the local history. Here the HEAD is pointed to the demo_repo/feature_demo_branch ref. As mentioned in the output, we can create a new local branch with the help of the git checkout command as follows:

$ git checkout -b local_of_feature_demo_branch 

Above, we have created a new local branch named local_of_feature_demo_branch, which will move HEAD to point at the latest remote content.


Syncing origin with git fetch

The following example shows how to synchronize the local repository with the central repository's master branch.

$ git fetch origin

The command above will display the branches that have been downloaded.

940d9d..d0be90c master -> origin/master 
dc6486..44d47bs develop -> origin/develop 
* [new branch] other-feature -> origin/other-feature

To see commits that have been added to the branch master, we can use the git log command and pass origin/master as a filter:

$ git log --oneline master..origin/master

To verify the changes and merge them into the local master branch, use the following commands:

$  git checkout master 
$ git log origin/master

Finally, we can run the git merge command to integrate change to the local working directory as follow:

$ git merge origin/master

Now the origin/master and master refs point to the same commit, and the local working directory is synchronized with the remote branch.


Git fetch vs. git pull

Both command git fetch and git pull download content from a remote repository. However, the git fetch command is considered the safe version of the two commands. The git fetch command will download the remote content, but it will not update the local repository. It will just show the progression of the central repository. On the other hand, the git pull command is an aggressive alternative. It downloads the content from the remote repository and directly integrates this content into the current Working Directory copy, which may cause merging conflicts.

Note: It is recommended to execute the git pull command only when the Working Directory copy is clean.



ExpectoCode is optimized for learning. Tutorials and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using this site, you agree to have read and accepted our terms of use, cookie and privacy policy.
Copyright 2020-2021 by ExpectoCode. All Rights Reserved.