LinuxCommandLibrary

git-fetch

Download objects and refs from another repository

TLDR

Fetch the latest changes from the default remote upstream repository (if set)

$ git fetch
copy

Fetch new branches from a specific remote upstream repository
$ git fetch [remote_name]
copy

Fetch the latest changes from all remote upstream repositories
$ git fetch --all
copy

Also fetch tags from the remote upstream repository
$ git fetch [[-t|--tags]]
copy

Delete local references to remote branches that have been deleted upstream
$ git fetch [[-p|--prune]]
copy

SYNOPSIS


git fetch [options] [repository] [refspec...]
git fetch [options] --all
git fetch [options] --prune

PARAMETERS

repository
    
The URL or name of the remote repository to fetch from. If omitted, all remotes are used.


refspec...
    
Specifies what refs to fetch and what local refs to update (e.g., +<src>:<dst>).


--all
    
Fetch all remotes defined in your configuration.


--prune, -p
    
Remove any remote-tracking branches that no longer exist on the remote.


--tags
    
Fetch all tags from the remote.


--no-tags
    
Disable automatic tag following; no tags are fetched.


--depth <depth>
    
Limit the fetch to the specified number of commits from branch tips.


--recurse-submodules
    
Recursively fetch all necessary data for submodules.


--force, -f
    
Force overwrite of local tracking branches even if they have diverged.


--dry-run, -n
    
Show what would be fetched without actually fetching.


DESCRIPTION


The `git fetch` command is used to download objects and refs from another repository, commonly a remote one configured in your Git setup. It retrieves all the branches and tags from the remote that are not already present in your local repository. Unlike `git pull`, `git fetch` does not automatically merge or rebase the fetched content into your current local branch. Instead, it updates your remote-tracking branches (e.g., `origin/main`, `origin/develop`), allowing you to inspect the changes on the remote without affecting your local working directory or current branch. This makes `git fetch` a safe operation to see what others have pushed. After fetching, you can then choose to merge, rebase, or cherry-pick specific changes into your local branches, giving you more control over integrating remote work.

CAVEATS


Unlike `git pull`, `git fetch` does not automatically update your current local branch or working directory. It only updates your remote-tracking branches (e.g., `origin/main`). To integrate changes into your local branch, you must explicitly run `git merge` or `git rebase` after fetching.
Using `--force` can overwrite local remote-tracking branches, even if they have diverged, so use it with caution.

FETCH_HEAD


After a successful `git fetch` operation, information about what was fetched is stored in the `.git/FETCH_HEAD` file. This file records the branch tips that were fetched from the remote repository. Commands like `git pull` use `FETCH_HEAD` to determine what to merge or rebase.

DEFAULT BEHAVIOR


When invoked without a repository or refspec, `git fetch` fetches all remote-tracking branches for all configured remotes that have `fetch` defined in their configuration. It also fetches any tags that point to newly fetched objects. This behavior is generally safe and allows you to see all recent changes on the remote without affecting your local work.

HISTORY


As a fundamental operation for interacting with remote repositories, `git fetch` has been a core command since Git's early days. Its primary role of downloading remote objects and references without modifying the local working directory has remained constant. Subsequent developments have introduced options such as `--prune`, `--depth`, and `--recurse-submodules` to provide more fine-grained control over what gets fetched and how, alongside performance optimizations for large repositories and network operations.

SEE ALSO

Copied to clipboard