Git fetch Command
In this page:
git fetch remote_name
git fetch remote_name branch_name
What is Git Fetch?
git fetch downloads new commits, branches, and tags from a remote repository into your local database -- but critically, it never touches your working files or current branch, making it a completely safe way to check what's changed.
Example: What is Git Fetch?
git fetch origin
Fetch vs Pull
The key difference from git pull is that fetch stops after downloading: it updates your remote-tracking branches (like origin/main) but leaves your actual local branches untouched, letting you review the incoming changes with git diff or git log before merging anything.
Example: Fetch vs Pull
git fetch origin
git log origin/main
Fetching Specific Branches
git fetch origin <branch> targets just one specific branch instead of every branch on the remote, which saves time and bandwidth on large repositories where you only care about updates to one line of work.
Example: Fetching Specific Branches
git fetch origin feature-x
Pruning Obsolete Branches
When teammates delete branches on the remote, your local remote-tracking references to them can become stale; git fetch --prune cleans those up automatically, deleting local references to branches that no longer exist on the remote.
Example: Pruning Obsolete Branches
git fetch --prune
Dry Run Fetching
git fetch --dry-run shows exactly what a real fetch would download without actually downloading it -- a lightweight way to test your connection or preview incoming changes before committing to the actual transfer.
Example: Dry Run Fetching
git fetch --dry-run
- Expecting
git fetchto change your files, when it only updates remote-tracking branches and leaves your working directory unchanged. - Running
git fetchand thinking you are up to date, when you still needgit merge origin/mainto bring the commits in. - Fetching a branch name that does not exist on the remote, like
git fetch origin featur, which gives acouldn't find remote referror.
Chapter Quiz — Complete all 11 topics to unlock
0/11 topics done
Complete these topics first: