How to use Git fetch

Greg Foster
Greg Foster
Graphite software engineer


Note

This guide explains this concept in vanilla Git. For Graphite documentation, see our CLI docs.


Understanding how to synchronize your local repository with a remote one is crucial. Thegit fetch command achieves this synchronization by telling your local Git to retrieve the latest meta-data info from the remote repository, but doesn't do any actual file transferring. It’s used to see the differences between your local state and the state of the remote repository. The command fetches branches and/or tags (collectively, "refs") from one or more other repositories, along with the meta-objects necessary to complete their histories.

This guide explains how to use the git fetch command, the differences between similar Git operations, and provide practical examples to get you started.

It's important to distinguish git fetch from git pull. While both commands are used to update the local version of a repository from a remote, git fetch updates your local copy of the remote repository's state without merging it with your local state. It lets you review these updates before deciding to merge.

On the other hand, git pull does a fetch under the hood, immediately followed by a merge/rebase, automatically integrating the remote changes with your current branch.

For more information on the git pull command see this guide on how to use git pull in your repository.

To fetch the latest updates from the remote repository, simply use:

Terminal
git fetch

This command fetches branches and tags from the default remote, usually origin. It's a safe way to review changes before integrating them into your local repository.

If you're working with multiple remotes, you can specify which one to fetch from:

Terminal
git fetch <remote-name>

This command fetches updates from a specified repository. Replace <remote-name> with the name of the remote repository you wish to fetch from.

Sometimes, you may only be interested in updates to a specific branch. In order to fetch from a specific branch run:

Terminal
git fetch <remote-name> <branch-name>

Replace <branch-name> with the branch name you're interested in, and <remote-name> with the name of the remote repository you want to fetch from.

  • Fetching all branches: To fetch all branches from the remote, use the -all option:

    git fetch --all

    This will fetch all branches from all of the remote repositories you’re currently tracking in your local working directory.

  • Fetching tags: To fetch tags from the remote repository, you can use the -tags option:

    git fetch --tags

    This will specifically fetch all of the tags from the remote repository.

  • Pruning: Over time, branches on the remote might be deleted. To reflect these deletions in your local repository, use the -prune option:

    git fetch --prune

    This command updates your local repository to reflect any branches that have been deleted on the remote repository.

After fetching, you can use git merge to merge the desired branch into your current branch, or git checkout to switch to a fetched branch. For example, to merge changes fetched from the origin/master into your current branch:

Terminal
git merge origin/master

Or, to check out a specific branch that you've fetched:

Terminal
git checkout <branch-name>

This will update your current working directory to reflect the state of the remote branch specified by <branch-name> .

  • Use git fetch when you want to see what others have committed to a repository but are not ready to integrate those changes into your own work.

  • Use git pull when you're ready to merge changes from the remote into your current working branch.

For further reading on the git fetch command, see the official Git documentation.

Graphite
Git stacked on GitHub

Stacked pull requests are easier to read, easier to write, and easier to manage.
Teams that stack ship better software, faster.

Or install our CLI.
Product Screenshot 1
Product Screenshot 2