
The git fetch
command allows you to review changes from a remote repository without altering your local branch. It updates the remote-tracking branches and saves the details in FETCH_HEAD
. After fetching, you can manually merge or rebase those changes into your local repository.
This article explains how to use git fetch
to update from remote repositories, including specific branches, all remotes, and cleanup tasks.
The Short Answer Version
If you’re already familiar with Git and 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
The following are some commonly used flags to retrieve updates from remote repositories and manage remote-tracking branches:
Flags | Description |
---|---|
--all |
Fetch from all remotes, not only the default remote, for example, origin . |
--prune |
Remove local references to branches deleted on the remote. |
--dry-run |
shows what updates are available without applying changes. |
--verbose |
Display detailed information about the git operation. |
--tags |
Fetches all tags from the remote along with branches. |
--force |
Force updates to local references to match the remote, even if it overwrites changes. |
--multiple |
Fetch from more than one remote in a single command. |
--atomic |
Either update all references, or none, to avoid partial updates. |
Fetch All Remote Updates
Use git fetch
command to retrieve the latest updates from a remote repository. This command shows all branches and commits in the remote repository and updates your remote-tracking branches without merging them into your local branches. It helps you review changes made by collaborators while keeping your local work unchanged until you choose to merge or rebase.
Command Syntax
git fetch <remote>
Where <remote>
is the remote repository you want to fetch changes from (for example, origin
).
Command Demonstration
Make a change in the remote repository by editing a file or creating a new commit.
In your local repository, verify the remote repository.
console$ git remote -v
It lists all remote repositories by name along with their URLs.
Output:
origin https://github.com/username/test-repo.git (fetch) origin https://github.com/username/test-repo.git (push)
Fetch updates from the remote repository.
console$ git fetch origin
It fetches updates from all branches on the
origin
remote and refreshes their remote-tracking branches.Inspect the fetched changes.
console$ git log origin/main
git log
displays the commit history of a 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 new changes
console$ git merge origin/main -m "commit message"
git merge
merges the changes you fetched.
Fetch a Specific Remote Branch
Using git fetch
with a branch name retrieves updates from a specific remote-tracking branch on the remote repository. This is useful when you only want to review or integrate changes from one feature or topic branch, instead of fetching updates for all branches.
Command Syntax
git fetch <remote> <branch>
Command Demonstration
Make changes to a remote branch in your remote repository and commit changes to the remote branch you want to fetch.
In your local repository, fetch a specific remote branch.
console$ git fetch origin feature-login
It fetches updates only from the
feature-login
branch on theorigin
remote and updates your remote-tracking branchorigin/feature-login
.Review the Fetched Branch
console$ git log origin/feature-login
It shows the commit history of the remote-tracking branch
origin/feature-login
.Merge the remote branch into your local branch
console$ git merge origin/feature-login -m "commit message"
It merges the changes you fetched from the remote
feature-login
branch into your current local branch.
Fetch From All Remotes
If you have multiple remote servers, you can use the --all
flag to fetch changes from all the remote servers. This updates the remote-tracking branches for all remotes, allowing you to review or merge changes.
$ git fetch --all
It fetches all changes from each remote and updates their remote-tracking branches so you can review or merge updates into your local branches.
--verbose
to see detailed information about which branches and commits are being updated.
Clean Up Deleted Remote Branches
Sometimes, the collaborators of your projects delete branches in your remote repository. You should remove them to avoid cluttering your branch list. You can use the git fetch --prune
command to remove these stale remote-tracking branches.
$ git fetch --prune
It removes references to remote branches that no longer exist, without touching your local branches.
Conclusion
You’ve learned to use git fetch
to review changes from remote repositories, fetch specific branches, update all remotes, and clean up deleted remote-tracking branches. These commands help you stay updated with collaborators’ work while keeping your local repository safe. For more details and advanced configuration options, refer to the official Git documentation.