mirror of
https://github.com/mintlify/docs.git
synced 2026-09-14 13:35:46 +08:00
324 lines
4.5 KiB
Plaintext
324 lines
4.5 KiB
Plaintext
---
|
|
title: git fetch
|
|
description: Download objects and refs from another repository
|
|
---
|
|
|
|
Download changes from a remote repository without merging them.
|
|
|
|
## Syntax
|
|
|
|
```bash
|
|
git fetch [options] [<repository>] [<refspec>]
|
|
```
|
|
|
|
## Description
|
|
|
|
`git fetch` downloads commits, files, and refs from a remote repository into your local repository. Unlike `git pull`, it doesn't merge changes into your working directory.
|
|
|
|
## Common usage
|
|
|
|
Fetch from default remote:
|
|
|
|
```bash
|
|
git fetch
|
|
```
|
|
|
|
Fetch from specific remote:
|
|
|
|
```bash
|
|
git fetch origin
|
|
```
|
|
|
|
## Options
|
|
|
|
<ParamField path="--all" type="flag">
|
|
Fetch from all remotes
|
|
|
|
```bash
|
|
git fetch --all
|
|
```
|
|
</ParamField>
|
|
|
|
<ParamField path="--prune" type="flag">
|
|
Remove remote-tracking branches that no longer exist
|
|
|
|
```bash
|
|
git fetch --prune
|
|
```
|
|
</ParamField>
|
|
|
|
<ParamField path="--tags" type="flag">
|
|
Fetch all tags
|
|
</ParamField>
|
|
|
|
<ParamField path="--dry-run" type="flag">
|
|
Show what would be fetched without fetching
|
|
</ParamField>
|
|
|
|
## Examples
|
|
|
|
### Basic fetch
|
|
|
|
```bash
|
|
git fetch
|
|
```
|
|
|
|
Downloads all branches from the default remote (usually `origin`).
|
|
|
|
### Fetch specific branch
|
|
|
|
```bash
|
|
git fetch origin main
|
|
```
|
|
|
|
### Fetch from all remotes
|
|
|
|
```bash
|
|
git fetch --all
|
|
```
|
|
|
|
### Fetch and prune
|
|
|
|
```bash
|
|
git fetch --prune
|
|
```
|
|
|
|
Removes local references to branches deleted on the remote.
|
|
|
|
### Fetch specific remote
|
|
|
|
```bash
|
|
git fetch upstream
|
|
```
|
|
|
|
## Fetch vs Pull
|
|
|
|
### git fetch
|
|
|
|
Downloads changes but doesn't merge:
|
|
|
|
```bash
|
|
git fetch
|
|
# Review changes
|
|
git merge origin/main
|
|
```
|
|
|
|
### git pull
|
|
|
|
Downloads and merges automatically:
|
|
|
|
```bash
|
|
git pull
|
|
# Equivalent to:
|
|
# git fetch + git merge origin/main
|
|
```
|
|
|
|
## Reviewing fetched changes
|
|
|
|
After fetching, review before merging:
|
|
|
|
```bash
|
|
# Fetch changes
|
|
git fetch origin
|
|
|
|
# Compare with remote
|
|
git diff main origin/main
|
|
|
|
# View remote commits
|
|
git log origin/main
|
|
|
|
# Merge when ready
|
|
git merge origin/main
|
|
```
|
|
|
|
## Common workflows
|
|
|
|
### Safe update workflow
|
|
|
|
```bash
|
|
# Fetch changes
|
|
git fetch origin
|
|
|
|
# Review what changed
|
|
git log ..origin/main
|
|
|
|
# Merge if satisfied
|
|
git merge origin/main
|
|
```
|
|
|
|
### Update all remotes
|
|
|
|
```bash
|
|
git fetch --all
|
|
```
|
|
|
|
### Check for updates without merging
|
|
|
|
```bash
|
|
git fetch
|
|
git status
|
|
```
|
|
|
|
Shows if your branch is behind the remote.
|
|
|
|
### Fetch specific branch
|
|
|
|
```bash
|
|
git fetch origin feature-branch
|
|
git checkout feature-branch
|
|
```
|
|
|
|
## Remote-tracking branches
|
|
|
|
Fetch updates remote-tracking branches:
|
|
|
|
```bash
|
|
git fetch origin
|
|
# Updates origin/main, origin/develop, etc.
|
|
|
|
# View remote branches
|
|
git branch -r
|
|
|
|
# Check out remote branch
|
|
git checkout -b local-branch origin/remote-branch
|
|
```
|
|
|
|
## Pruning deleted branches
|
|
|
|
When branches are deleted on remote:
|
|
|
|
```bash
|
|
# Remove stale references
|
|
git fetch --prune
|
|
|
|
# Or configure automatic pruning
|
|
git config --global fetch.prune true
|
|
```
|
|
|
|
## Advanced usage
|
|
|
|
### Fetch all tags
|
|
|
|
```bash
|
|
git fetch --tags
|
|
```
|
|
|
|
### Fetch specific tag
|
|
|
|
```bash
|
|
git fetch origin tag v1.0
|
|
```
|
|
|
|
### Shallow fetch
|
|
|
|
Limit history depth:
|
|
|
|
```bash
|
|
git fetch --depth=1
|
|
```
|
|
|
|
### Dry run
|
|
|
|
See what would be fetched:
|
|
|
|
```bash
|
|
git fetch --dry-run
|
|
```
|
|
|
|
## Fetch strategies
|
|
|
|
### Fetch often
|
|
|
|
Stay updated with team changes:
|
|
|
|
```bash
|
|
# Fetch regularly
|
|
git fetch origin
|
|
|
|
# Check for updates
|
|
git status
|
|
```
|
|
|
|
### Fetch before pushing
|
|
|
|
Ensure you have latest changes:
|
|
|
|
```bash
|
|
git fetch origin
|
|
git rebase origin/main
|
|
git push
|
|
```
|
|
|
|
### Fetch before branching
|
|
|
|
Start with latest code:
|
|
|
|
```bash
|
|
git fetch origin
|
|
git checkout -b new-feature origin/main
|
|
```
|
|
|
|
## Troubleshooting
|
|
|
|
### Permission denied
|
|
|
|
Check authentication:
|
|
|
|
```bash
|
|
# Test SSH connection
|
|
ssh -T git@github.com
|
|
|
|
# Or update remote URL
|
|
git remote set-url origin <correct-url>
|
|
```
|
|
|
|
### Timeout issues
|
|
|
|
Increase timeout:
|
|
|
|
```bash
|
|
git config --global http.lowSpeedLimit 0
|
|
git config --global http.lowSpeedTime 999999
|
|
```
|
|
|
|
### Wrong remote
|
|
|
|
Verify remotes:
|
|
|
|
```bash
|
|
git remote -v
|
|
git fetch <correct-remote>
|
|
```
|
|
|
|
## Best practices
|
|
|
|
- Fetch regularly to stay updated
|
|
- Use `fetch` instead of `pull` for safer updates
|
|
- Review changes before merging
|
|
- Enable automatic pruning
|
|
- Fetch from `upstream` in fork workflows
|
|
- Use `--all` to update all remotes
|
|
- Fetch before creating new branches
|
|
|
|
## Configuration
|
|
|
|
### Auto-prune on fetch
|
|
|
|
```bash
|
|
git config --global fetch.prune true
|
|
```
|
|
|
|
### Default remote
|
|
|
|
```bash
|
|
git config branch.main.remote origin
|
|
```
|
|
|
|
## Related commands
|
|
|
|
- [git pull](/git/pull) - Fetch and merge
|
|
- [git merge](/git/merge) - Combine branches
|
|
- [git rebase](/git/rebase) - Reapply commits
|
|
- [git remote](/git/remote) - Manage remotes
|
|
- [git branch](/git/branch) - View branches
|