Skip to main content

Git Fetch Explained – How to Download from Remote Repos

Git fetching is mainly about getting (downloading) files, commits, and refs from a project's remote repository to the local repo you are working on.

For instance, suppose you are working on a team project and wish to see your colleagues' work. In such a case, fetching from the remote repo lets you download and see your teammates' progress.

Below are the main ways developers fetch from remote directories.

How to Fetch a Remote Repository's Content

>_ Terminal
git fetch <theRemoteRepo>

The command above will download <theRemoteRepo>'s content to your local repository.

note

Replace <theRemoteRepo> with the remote repository you want to fetch. For instance, suppose origin is the remote repo's name. In that case, you would run git fetch origin.

How to Fetch from a Specific Branch of a Remote Repository

>_ Terminal
git fetch <theRemoteRepo> <theSpecificBranch>

The command above will download the content of <theSpecificBranch> only.

note
  • Replace <theRemoteRepo> with the remote repository you want to fetch.
  • Replace <theSpecificBranch> with the branch you want to fetch.

Important Stuff to Know about Git Fetching

  • Git separates the fetched content from your local work.
  • Your local work lives in your local repo's ./.git/refs/heads/ directory.
  • The remote work you fetched lives in your local repo's ./.git/refs/remotes/ directory.
  • Git automatically maps your fetched content to its remote-tracking branch. The syntax it uses to name the remote-tracking branch is remote/remote-repo-name/branch-name.
  • Use the git branch -a command to view your local and remote branches.
  • Use the git branch -r command to view only the remote branches.

How to Switch to a Remote-Tracking Branch

Use git checkout to switch to the remote-tracking branch (the branch containing your fetched content).

>_ Terminal
git checkout remote-repo-name/branch-name

The command above tells Git to switch to remote-repo-name/branch-name.

note
  • Replace remote-repo-name with the remote repo's URL name. For instance, a remote repository's URL name could be origin, central-repo, or company-repo.
  • Replace branch-name with the name of the branch you fetched.

After running the git checkout command, Git will put you in a detached HEAD state.

How to Merge a Fetched Branch with a Local Branch

You can use the git merge command to merge your fetched content with your local branch.

important

Before invoking the merge command, ensure the active branch is the branch you want to merge into.

In other words, switch to the branch you wish to update before running the merge command.

>_ Terminal
git merge name-of-the-fetched-branch

The code above instructs Git to merge name-of-the-fetched-branch with the HEAD (current) branch.

Git Fetch vs. Git Pull: What's the Difference?

git fetch downloads a remote repository to your local repo without merging the differences between the two directories. Instead, Git separates the fetched content from your local work by putting the downloaded data in your local repo's ./.git/refs/remotes/ directory.

In other words, git fetch provides a safe way to download a remote repo without altering your local repository. Therefore, you can review the fetched content and decide whether to merge some (or all) of its commits with your local work.

git pull downloads a remote repository to your local repo. Afterward, it automatically uses the git merge command to merge the differences between the two directories.

Suppose the merge command encounters conflicts. In that case, Git will auto-start the merge conflict resolution process.

In other words, git pull provides a quick way to download and merge a remote repo with your local repository.

Here is git pull's syntax:

>_ Terminal
git pull <theRemoteRepo>

The command above will download and merge <theRemoteRepo>'s content with your local repository.

note

Replace <theRemoteRepo> with the remote repository you want to pull. For instance, suppose origin is the remote repo's name. In that case, you would run git pull origin.

Overview

This article discussed the main ways to fetch a remote repository's content.

FREE Git Cheat Sheet!

Get the CodeSweetly free Git PDF cheat sheet.

Use it to easily remember the Git commands you need for your projects.

Download Now

Tweet this article