Understanding the git fetch
Command¶
The git fetch
command helps you see what’s new in a remote repository without modifying any of your local branches. It’s like checking the mail without opening any letters.
1. What Does git fetch
Do?¶
- Retrieves the latest changes (commits, branches, tags) from a remote repository.
- Updates your local metadata (remote tracking branches), but does not merge or apply these changes to your working branches.
- It’s safe: you can fetch as often as you like without worrying about overwriting local changes.
Analogy: If
git pull
is like receiving new mail and reading it right away,git fetch
is like receiving new mail and putting it aside to read later.
2. Common Use Cases¶
- Check for new commits on the remote before merging changes locally.
- Review differences between your local branch and the remote branch.
- Fetch updates from multiple remotes in complex setups (e.g.,
origin
+upstream
).
3. Basic Usage¶
3.1 Fetch from the Default Remote¶
- Fetches updates from all configured remotes (commonlyorigin
).
- If you have only one remote, it’s effectively the same as git fetch origin
.
3.2 Fetch from a Specific Remote¶
- Fetches the latest updates only from the remote namedorigin
.
- Updates all branches in origin
(e.g., origin/main
, origin/feature-xyz
).
- Does not update local branches automatically.
3.3 Fetch Only a Specific Branch¶
- Fetches only themain
branch from the remote named origin
.
- Updates only origin/main
, without touching other branches.
4. Differences at a Glance¶
Command | Fetches Updates From | Updates Which Branches? | Use Case |
---|---|---|---|
git fetch |
All remotes | All remote tracking branches | Check updates from all remotes |
git fetch origin |
origin only |
All branches in origin |
Fetch all branches from origin |
git fetch origin main |
origin only |
Only origin/main |
Quickly fetch updates just for the main branch |
git fetch
: Good if you have multiple remotes or want to update all remote branches.git fetch origin
: Focuses on fetching all branches fromorigin
only.git fetch origin main
: Ideal if you care only about themain
branch.
5. Advanced Options¶
5.1 Fetch All Remotes¶
- Retrieves updates from every remote (e.g.,origin
, upstream
).
5.2 Prune Deleted Branches¶
- Removes remote-tracking branches in your local repo that no longer exist on the remote. - Combine both:5.3 Verbose Output¶
- Shows detailed information about what’s being fetched.6. Viewing Fetched Changes¶
After fetching, compare your local branch to the fetched remote branch:
- Shows commits that exist inorigin/main
but not in your local main
.
Or use git diff
:
7. Applying Fetched Changes¶
Since fetching alone doesn’t modify your local branches, you can:
- Pull (merge automatically)
- Merge (manual merge)
- Rebase
8. Example Scenarios¶
8.1 Quick Check Before Pulling¶
- Fetch the latest changes:
- Inspect what’s new:
- Pull if needed:
8.2 Multiple Remotes¶
- You fork a repository, so you have:
origin
→ Your fork.upstream
→ The original repo.- Fetch updates from both:
- Merge changes from
upstream
if necessary.
8.3 Checking a Single Branch¶
- Fetches only the specified branch fromorigin
.
9. Key Takeaways¶
git fetch
is a safe way to update your local knowledge of remote branches without modifying your local branches.- Use
git fetch --all --prune
to keep your local copy of remote branches clean. - Use
git fetch origin <branch>
if you only need updates for a specific branch. - If you want those fetched changes in your local branch, merge, pull, or rebase.
Happy Fetching! 🚀