The git fetch
command downloads commits, refs, and other information from a remote repository so you can review the changes before applying them to your local repository. This is useful for keeping your local repository up to date with the latest changes from your team.
Initial Example
To fetch updates from the remote repository, use:
git fetch
FETCH Options
Option | Description |
---|---|
--all | Fetch all remotes, except for those excluded by the remote..skipFetchAll configuration variable. |
-a, --append | Append ref names and object names of fetched refs to the existing contents of .git/FETCH_HEAD. |
--atomic | Use an atomic transaction to update local refs. Either all refs are updated, or none are. |
--depth=<depth> | Limit fetching to the specified number of commits from the tip of each remote branch history. |
--deepen=<depth> | Limit fetching to the specified number of commits from the current shallow boundary. |
--shallow-since=<date> | Deepen or shorten the history to include all reachable commits after the specified date. |
--shallow-exclude=<revision> | Exclude commits reachable from the specified remote branch or tag. |
--unshallow | Convert a shallow repository to a complete one by fetching as much as possible. |
--tags | Fetch all tags from the remote repository. |
--no-tags | Disable automatic tag following. |
--dry-run | Perform a trial run to show what would be fetched without making any actual changes. |
--multiple | Allow multiple repository or group arguments. |
--negotiate-only | Print the ancestors of the provided negotiation tips without fetching anything. |
--negotiation-tip=<commit|glob> | Report commits reachable from the given tips to reduce the size of the to-be-received packfile. |
Examples
1. Fetching Updates from the Default Remote
To download changes from the default remote repository (usually named origin
), simply use:
git fetch
This command fetches new data and updates the remote-tracking branches.
2. Fetching Updates from a Specific Remote
If you have multiple remote repositories, you can specify which one to fetch from:
git fetch <remote_name>
For example, to fetch from a remote named upstream
:
git fetch upstream
3. Fetching a Specific Branch
To fetch updates for a specific branch from a remote repository, use:
git fetch <remote_name> <branch_name>
For example, to fetch updates for the develop
branch from origin
:
git fetch origin develop
4. Fetching All Updates and Tags
To fetch all branches and tags from the remote repository, use:
git fetch --all --tags
This command ensures that your local repository has the latest information from all branches and tags.