How to Fetch All Branches in Git: Simple Commands Explained
To fetch all branches from a remote repository in Git, use the command
git fetch --all. This downloads all branches and updates your local references without merging changes.Syntax
The basic command to fetch all branches from all remotes is git fetch --all. Here:
git fetchdownloads commits, files, and refs from a remote repository.--alltells Git to fetch from all configured remotes, not just one.
You can also fetch from a specific remote using git fetch <remote-name>.
bash
git fetch --all
Example
This example shows fetching all branches from the default remote named origin. It updates your local copy of all remote branches without merging them into your current branch.
bash
git fetch origin
Output
From github.com:user/repo
* [new branch] feature-x -> origin/feature-x
* [new branch] bugfix-123 -> origin/bugfix-123
* [new branch] develop -> origin/develop
Common Pitfalls
One common mistake is using git pull when you only want to fetch branches. git pull fetches and merges, which can cause unwanted changes in your working directory.
Another pitfall is forgetting to specify the remote if you have multiple remotes configured. git fetch --all fetches from all remotes, but git fetch alone fetches only from the default remote.
bash
git pull # merges changes immediately # Correct way to only fetch all branches without merging git fetch --all
Quick Reference
| Command | Description |
|---|---|
| git fetch --all | Fetch all branches from all remotes |
| git fetch origin | Fetch all branches from the remote named origin |
| git fetch origin feature-x | Fetch only the branch feature-x from origin |
| git pull | Fetch and merge changes (not recommended if you only want to fetch) |
Key Takeaways
Use
git fetch --all to download all branches from all remotes without merging.To fetch from a specific remote, use
git fetch <remote-name>.Avoid
git pull if you only want to fetch branches to prevent automatic merges.Fetching updates your local remote-tracking branches but does not change your working files.
Check your remotes with
git remote -v to know where you fetch from.