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.
Git fetch vs. pull: understanding the difference
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.
Using the git fetch command
Basic fetching
To fetch the latest updates from the remote repository, simply use:
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.
Fetching from a specific remote
If you're working with multiple remotes, you can specify which one to fetch from:
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.
Fetching a specific branch
Sometimes, you may only be interested in updates to a specific branch. In order to fetch from a specific branch run:
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.
Advanced fetching options
Fetching all branches: To fetch all branches from the remote, use the
-alloption:git fetch --allThis 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
-tagsoption:git fetch --tagsThis 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
-pruneoption:git fetch --pruneThis command updates your local repository to reflect any branches that have been deleted on the remote repository.
After fetching: what’s next?
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:
git merge origin/master
Or, to check out a specific branch that you've fetched:
git checkout <branch-name>
This will update your current working directory to reflect the state of the remote branch specified by <branch-name> .
When to use fetch vs. pull
Use
git fetchwhen 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 pullwhen 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.