How to fetch all branches in Git
Fetching all branches downloads branch references and commits from remote repository without merging them into local branches, enabling safe inspection before integration. As the creator of CoreUI, a widely used open-source UI library, I’ve managed multi-branch workflows in distributed teams throughout my 25 years of development experience. The most comprehensive approach is using git fetch –all to retrieve updates from all configured remotes with prune option to remove stale references. This method synchronizes local repository with remote state, updates tracking branches, and cleans up deleted remote branches automatically.
Use git fetch –all to download all branches from all remotes without merging.
# Fetch all branches from all remotes
git fetch --all
# Fetch all branches from specific remote
git fetch origin
# Fetch all branches and prune deleted remote branches
git fetch --all --prune
# Fetch all branches with verbose output
git fetch --all --verbose
# View all remote branches after fetching
git branch -r
# View all branches including remote tracking branches
git branch -a
Working with fetched branches:
# Fetch all branches from origin
git fetch origin
# List all remote branches
git branch -r
# Output:
# origin/main
# origin/develop
# origin/feature/new-ui
# origin/bugfix/login-issue
# Create local branch from remote branch
git checkout -b feature/new-ui origin/feature/new-ui
# Or use simpler syntax (Git 2.23+)
git switch -c feature/new-ui origin/feature/new-ui
# Track remote branch explicitly
git branch --track feature/new-ui origin/feature/new-ui
# Update existing local branch with remote changes
git fetch origin
git checkout feature/new-ui
git merge origin/feature/new-ui
# Or use pull (fetch + merge)
git pull origin feature/new-ui
Fetch all branches and prune stale references:
# Fetch with prune to remove deleted remote branches
git fetch --all --prune
# Configure automatic pruning for all fetches
git config --global fetch.prune true
# View pruned branches
git remote prune origin --dry-run
# Actually prune stale remote-tracking branches
git remote prune origin
# Fetch all branches with tags
git fetch --all --tags
# Fetch specific branches only
git fetch origin main develop feature/new-ui
Inspect fetched branches before merging:
# View commits in remote branch not in current branch
git log HEAD..origin/main
# View commits in current branch not in remote branch
git log origin/main..HEAD
# View differences between local and remote branch
git diff main origin/main
# View all changes from remote branch
git log -p origin/main
# View remote branch commit history
git log origin/feature/new-ui --oneline --graph
# Compare multiple branches
git log --oneline --graph --all --decorate
Fetch configuration and multiple remotes:
# Add multiple remotes
git remote add upstream https://github.com/original/repo.git
git remote add fork https://github.com/yourname/repo.git
# Fetch from all remotes
git fetch --all
# This fetches from: origin, upstream, fork
# Fetch only from specific remote
git fetch upstream
# View all configured remotes
git remote -v
# Output:
# origin https://github.com/yourname/repo.git (fetch)
# origin https://github.com/yourname/repo.git (push)
# upstream https://github.com/original/repo.git (fetch)
# upstream https://github.com/original/repo.git (push)
# Configure fetch refspec for specific branches
git config remote.origin.fetch '+refs/heads/main:refs/remotes/origin/main'
git config --add remote.origin.fetch '+refs/heads/develop:refs/remotes/origin/develop'
# Set default remote for current branch
git branch --set-upstream-to=origin/main main
Fetch strategies for different workflows:
# Regular synchronization workflow
git fetch --all --prune
git branch -r
git checkout develop
git merge origin/develop
# Review changes before merging
git fetch origin
git log HEAD..origin/main --oneline
git diff HEAD origin/main
git merge origin/main
# Fetch and rebase workflow
git fetch origin
git rebase origin/main
# Shallow fetch for large repositories
git fetch --depth=1 origin
# Fetch with deepen for shallow clones
git fetch --deepen=50
# Fetch unshallow to get complete history
git fetch --unshallow
Automate fetching with Git hooks:
# Create post-merge hook to fetch after merge
cat > .git/hooks/post-merge << 'EOF'
#!/bin/bash
git fetch --all --prune
echo "Fetched all branches and pruned stale references"
EOF
chmod +x .git/hooks/post-merge
Here git fetch –all downloads branch and tag references from all configured remotes without modifying working directory. The –prune flag removes remote-tracking branches that no longer exist on remote repository. The git branch -r command lists remote-tracking branches stored in refs/remotes directory. The checkout -b creates local branch tracking remote branch for local development. The fetch operation downloads commits and updates remote-tracking refs without changing HEAD or working tree. The log HEAD..origin/main shows commits in remote branch not present in local branch. The diff command compares file changes between local and remote branches before merging.
Best Practice Note:
This is the branch fetching workflow we use in CoreUI repository to synchronize with contributor forks and keep local repository current. Fetch regularly before starting new work to avoid working on outdated code and merge conflicts, use git fetch instead of git pull to review changes before integrating them into your branch, and configure fetch.prune to automatically clean up deleted remote branches and maintain clean branch list in your local repository.



