Git Fetch Commands
Overview
The git fetch
command downloads objects and refs from another repository. This command is essential for updating your local repository with the latest changes from a remote repository without merging them into your local branch. This document covers the basic and advanced usage of git fetch
.
Basic Fetch
git fetch
Fetches updates from the default remote repository (usually origin
).
Example
This command fetches updates from the origin
remote repository without merging them into your local branch.
git fetch <remote>
Fetches updates from a specific remote repository.
Example
This command fetches updates from the origin
remote repository.
Fetching Specific Branches
git fetch <remote> <branch>
Fetches updates from a specific branch of a remote repository.
Example
This command fetches updates from the main
branch of the origin
remote repository.
Fetching All Branches
git fetch --all
Fetches updates from all configured remotes and their branches.
Example
This command fetches updates from all remote repositories and their branches configured in your local repository.
Pruning Deleted Branches
git fetch --prune
Prunes (removes) tracking references that no longer exist on the remote.
Example
This command removes any tracking branches that have been deleted on the remote repository.
Fetching Tags
git fetch --tags
Fetches all tags from the remote repository.
Example
This command fetches all tags from the remote repository.
Fetching with Depth
git fetch --depth <depth>
Fetches only the specified number of commits from the remote repository.
Example
This command fetches only the latest commit from the remote repository.
Specifying Refspecs
git fetch <remote> <refspec>
Fetches a specific refspec from a remote repository.
Example
This command fetches the main
branch from the origin
remote and updates the corresponding remote-tracking branch in your local repository.
Fetching All Remote Branches
git fetch <remote> +refs/heads/*:refs/remotes/<remote>/*
Fetches all branches from a remote repository and updates the corresponding remote-tracking branches.
Example
This command fetches all branches from the origin
remote and updates the corresponding remote-tracking branches in your local repository.
Fetch and Merge
git fetch
and git merge
Fetches changes from a remote repository and merges them into the current branch in two separate steps.
Example
This sequence of commands fetches changes from the main
branch of the origin
remote and merges them into the current branch.
Summary
The git fetch
command is a powerful tool for updating your local repository with the latest changes from a remote repository without merging them immediately. This allows you to review changes before integrating them into your work. For more detailed information on each command, refer to the official Git documentation.