Git Fetch Command: How to Update from Remote Repositories
The git fetch command allows you to view updates from a remote repository without changing your local branches. It updates the remote-tracking branches and saves the information in FETCH_HEAD. After fetching, you can decide to merge or rebase those updates into your local repository.
This guide explains how to use git fetch to pull updates from remote repositories — including specific branches, all remotes, and cleanup tasks for deleted branches.
The Short Answer Version
If you already know Git and just need quick reference commands for fetching updates:
# Fetch updates from a specific remote
$ git fetch <remote>
# Fetch updates of a specific branch from a remote
$ git fetch <remote> <branch>
# Fetch updates from all remotes
$ git fetch --all
# Remove local references to deleted remote branches
$ git fetch --prune
Common Options for git fetch
Here are some frequently used flags for retrieving updates from remotes and managing remote-tracking branches:
| Flag | Description |
|---|---|
| –all | Fetches updates from all remotes, not just the default one (e.g., origin). |
| –prune | Removes local references to branches deleted on the remote. |
| –dry-run | Shows available updates without applying them. |
| –verbose | Displays detailed information during the operation. |
| –tags | Fetches all tags along with branches. |
| –force | Forces updates to match the remote, even if it overwrites local references. |
| –multiple | Fetches from multiple remotes in one command. |
| –atomic | Ensures all references update together or none at all to prevent partial updates. |
Fetch All Remote Updates
Use git fetch to download the latest commits and branches from a remote repository. This operation updates your remote-tracking branches without merging them into your local ones, allowing you to review changes before applying them.
Command Syntax
git fetch <remote>
Here, <remote> refers to the repository you want to fetch from (for example, origin).
Command Demonstration
First, make a change in your remote repository — such as editing a file or creating a new commit. Then, check your configured remotes locally:
$ git remote -v
This lists all remotes with their associated URLs.
Example Output
origin https://github.com/username/test-repo.git (fetch)
origin https://github.com/username/test-repo.git (push)
Now, fetch the latest updates from the remote repository:
$ git fetch origin
This retrieves updates from all branches in origin and updates their remote-tracking branches.
Inspect the Fetched Changes
$ git log origin/main
The git log command shows the commit history of the branch.
Sample Output
commit 8ecb58dbecee84f9fd0b2e0089df7dae63814be4 (origin/main, origin/HEAD)
Author: username <username@gmail.com>
Date: Sun Sep 7 22:14:54 2025 +0530
New Line Added
Merge the Fetched Changes
$ git merge origin/main -m "commit message"
The git merge command merges fetched changes into your local branch.
Fetch a Specific Remote Branch
To retrieve updates from a specific branch, include the branch name in your fetch command. This lets you fetch changes for a particular feature or topic branch without updating all branches.
Command Syntax
git fetch <remote> <branch>
Command Demonstration
After committing changes to a specific remote branch, fetch it locally:
$ git fetch origin feature-login
This fetches only the updates from the feature-login branch and updates origin/feature-login.
Review the Fetched Branch
$ git log origin/feature-login
This shows the commit history of the fetched branch.
Merge the Remote Branch into Your Local Branch
$ git merge origin/feature-login -m "commit message"
This merges the updates from the remote feature-login branch into your current branch.
Fetch from All Remotes
If your project uses multiple remotes, add the --all flag to fetch updates from each one simultaneously. This ensures all remote-tracking branches stay up to date.
$ git fetch --all
This downloads changes from every configured remote. You can also include --verbose for detailed output.
Clean Up Deleted Remote Branches
When branches are deleted from the remote repository, run git fetch --prune to remove outdated references and keep your branch list clean.
$ git fetch --prune
This removes references to deleted remote branches without altering your local branches.
Conclusion
This article covered how to use git fetch to view updates from remote repositories, fetch specific branches, update all remotes, and clean up outdated branch references. Using these commands keeps your local work synced and organized while avoiding unintentional changes to your local branches. For advanced configurations, refer to the official Git documentation.


