fetch Git Command Guide
The git fetch command is used to download objects and refs from one or more remote repositories, along with the objects necessary to complete their histories. It updates remote-tracking branches and stores fetched information in .git/FETCH_HEAD.
git fetch Syntax:
Section titled “git fetch Syntax:”git fetch [<options>] [<repository> [<refspec>...]]Options:
Section titled “Options:”Parameters:
Section titled “Parameters:”| Parameter | Description |
|---|---|
| [ | Remote repository to fetch from (default: origin) |
| [ | Refs to fetch (default: configured fetch refspec) |
git fetch Command Samples:
Section titled “git fetch Command Samples:”Fetch from default remote
Section titled “Fetch from default remote”git fetchFetch changes from the default remote (usually origin).
Fetch from specific remote
Section titled “Fetch from specific remote”git fetch originFetch from the origin remote.
Fetch specific branch
Section titled “Fetch specific branch”git fetch origin mainFetch only the main branch from origin.
Fetch all remotes
Section titled “Fetch all remotes”git fetch --allFetch from all configured remotes.
Prune obsolete refs
Section titled “Prune obsolete refs”git fetch --pruneRemove local remote-tracking refs that no longer exist remotely.
Shallow fetch with depth
Section titled “Shallow fetch with depth”git fetch --depth=1 originFetch with shallow history of depth 1.
Dry run to see what would be fetched
Section titled “Dry run to see what would be fetched”git fetch --dry-run originShow what would be fetched without making changes.
Fetch all tags
Section titled “Fetch all tags”git fetch --tags originFetch all tags from origin in addition to normal refs.
Convert shallow to full repository
Section titled “Convert shallow to full repository”git fetch --unshallow originConvert a shallow clone to a full repository.
Atomic fetch with commit graph
Section titled “Atomic fetch with commit graph”git fetch --atomic --write-commit-graph originUse atomic transaction and write commit-graph for improved performance.
How do I update my local branches with remote changes?
Section titled “How do I update my local branches with remote changes?”To update your local branches with remote changes, run:
git fetch originHow can I fetch from all remotes at once?
Section titled “How can I fetch from all remotes at once?”To fetch from all configured remotes, use:
git fetch --allHow do I clean up old remote-tracking branches?
Section titled “How do I clean up old remote-tracking branches?”To clean up old remote-tracking branches, use —prune:
git fetch --prune originHow can I preview what fetch will do?
Section titled “How can I preview what fetch will do?”To preview what git fetch will do, use —dry-run:
git fetch --dry-run originHow do I fetch a shallow copy?
Section titled “How do I fetch a shallow copy?”To fetch a shallow copy with limited history, use —depth:
git fetch --depth=<n> originHow can I make a shallow repository full?
Section titled “How can I make a shallow repository full?”To make a shallow repository full, use —unshallow:
git fetch --unshallow originHow do I force a fetch that would otherwise be rejected?
Section titled “How do I force a fetch that would otherwise be rejected?”To force a fetch that would be rejected due to safety checks, use —force:
git fetch --force originHow can I fetch all tags from remote?
Section titled “How can I fetch all tags from remote?”To fetch all tags from the remote, use —tags:
git fetch --tags originHow do I prevent automatic tag fetching?
Section titled “How do I prevent automatic tag fetching?”To prevent automatic tag fetching, use —no-tags:
git fetch --no-tags originApplications of the git fetch command
Section titled “Applications of the git fetch command”- Updating repositories with remote changes before merging
- Setting up tracking branches for collaboration
- Managing multiple remote repositories in a project
- Implementing continuous integration fetch operations
- Creating backup and mirror repositories
- Supporting distributed development workflows