The git fetch command is used to download information about changes from a remote repository without directly integrating those changes into your current working branch.
git fetch is useful when you want
to see what changed on the remote repository before deciding whether
and how to integrate those changes.
git fetch downloads references and objects from a remote
repository and updates the corresponding remote-tracking branches.
Remote Repository
↓
git fetch
↓
Local Repository
↓
Remote-tracking Branches
Git fetch is useful when you want to inspect remote changes before integrating them into your current branch.
The basic syntax is:
git fetch <remote>
For example:
git fetch origin
This fetches updates from the remote named
origin.
origin is commonly the name assigned to the main remote
repository.
git fetch origin
Git retrieves information from that remote and updates its remote-tracking references.
Use the following command to view configured remotes:
git remote -v
Example:
origin https://github.com/username/project.git (fetch)
origin https://github.com/username/project.git (push)
You can fetch a specific branch from a remote:
git fetch origin main
This retrieves information associated with the
main branch from origin.
If your repository has multiple remotes, you can fetch from all of them:
git fetch --all
This is useful when you work with multiple remote repositories.
One important feature of git fetch is that it does not
automatically merge the fetched changes into your current branch.
git fetch origin
After fetching, you can inspect the changes and decide how to integrate them.
| git fetch | git pull |
|---|---|
| Downloads remote changes | Downloads and integrates remote changes |
| Does not directly update the current branch | Can update the current branch |
| Useful for reviewing changes first | Useful for synchronizing a branch |
After fetching, Git updates remote-tracking branches such as:
origin/main
origin/feature-login
These references represent the state of the corresponding branches as last known from the remote.
Use:
git branch -r
Example:
origin/main
origin/develop
origin/feature-login
The -r option displays remote-tracking branches.
Use:
git branch -a
This displays both local branches and remote-tracking branches.
* main
feature-login
remotes/origin/main
remotes/origin/feature-login
A common workflow is:
git fetch origin
git log main..origin/main
The second command can show commits that exist on
origin/main but not on the local main branch.
After fetching, you can compare your local branch with the remote-tracking branch.
git diff main origin/main
This helps you inspect file-level differences before integrating changes.
After fetching, use:
git log origin/main
This allows you to inspect the commit history currently known for the remote-tracking branch.
If your Git repository is connected to GitHub, you can fetch updates using:
git fetch origin
The remote information is downloaded to your local repository without automatically changing the files in your current working branch.
A repository may also have a remote named
upstream.
git fetch upstream
This is commonly used when working with a fork and an original repository.
Remote branches that were deleted from the server can leave stale remote-tracking references locally.
You can clean up stale references with:
git fetch --prune
You can also combine it with a remote:
git fetch origin --prune
Pruning removes local remote-tracking references for remote branches that no longer exist on the remote repository.
For example, if a remote branch is deleted:
origin/old-feature
a prune operation can remove its stale local reference.
Fetching does not normally replace the files in your current working directory with the remote branch's files.
For example:
git fetch origin
You can inspect the fetched information before deciding whether to merge, rebase, or otherwise integrate it.
You can explicitly fetch remote changes and then merge them:
git fetch origin
git merge origin/main
This separates the retrieval step from the integration step.
Fetching can also be used before rebasing local work onto a remote branch:
git fetch origin
git rebase origin/main
This gives you explicit control over when the remote information is retrieved and how your local commits are integrated.
Because fetching does not directly modify the current working files, it is generally useful even when you have local work that has not been committed.
git status
git fetch origin
However, you should still review your local state before performing a later merge or rebase.
In a team project, developers can use fetch to see whether teammates have pushed new commits.
git fetch origin
git log HEAD..origin/main
This helps you inspect changes before integrating them into your current work.
git fetch
git fetch origin
git fetch origin main
git fetch --all
git fetch --prune
git fetch origin --prune
Choose the command according to which remote and branches you need to update.
Check the remote configuration with:
git remote -v
Using fetch:
git fetch origin
git log main..origin/main
git diff main origin/main
git merge origin/main
Here you inspect the remote changes before integrating them.
Using pull:
git pull origin main
The pull command performs retrieval and then an integration step.
git status
git fetch origin
git branch -r
git log main..origin/main
git diff main origin/main
git merge origin/main
This workflow gives you an opportunity to inspect remote changes before integrating them into the local branch.
The git fetch command retrieves information from a
remote repository without directly integrating those changes into the
current local branch.
git fetch origin
A useful workflow is:
git fetch origin
git log main..origin/main
git diff main origin/main
git merge origin/main
Using fetch gives you more control because you can inspect remote changes before integrating them.
git fetch retrieves changes from a remote repository.git fetch origin fetches from the origin remote.git fetch --all fetches from all configured remotes.git fetch --prune removes stale remote-tracking references.git branch -r displays remote-tracking branches.git branch -a displays local and remote branches.git log and git diff.git fetch gives more control than directly using git pull.Question: What is the main purpose of git fetch?