How to fetch specific branch in Git
Fetching a specific branch in Git downloads only that branch’s commits from the remote repository without affecting your working directory. As the creator of CoreUI with 26 years of development experience, I’ve optimized Git workflows across hundreds of repositories by fetching only needed branches, reducing bandwidth usage and speeding up synchronization in large projects with many feature branches.
The fastest way is using git fetch with the branch name specified.
Fetch Specific Branch
# Fetch specific branch from origin
git fetch origin feature-branch
# This downloads commits but doesn't merge them
# Branch is available as origin/feature-branch
Fetch and Checkout
# Fetch specific branch and check it out
git fetch origin feature-branch
git checkout feature-branch
# Or in one command
git fetch origin feature-branch:feature-branch
git checkout feature-branch
Fetch Multiple Specific Branches
# Fetch multiple branches
git fetch origin main develop feature-x
# Verify fetched branches
git branch -r
Fetch Branch from Different Remote
# Add remote
git remote add upstream https://github.com/original/repo.git
# Fetch specific branch from upstream
git fetch upstream main
# Check out as local branch
git checkout -b upstream-main upstream/main
Fetch Without Updating Remote Tracking
# Fetch branch without updating tracking info
git fetch origin feature-branch --no-tags
# Fetch only commits, skip tags
git fetch origin feature-branch --no-tags --depth=1
Shallow Fetch (Latest Commits Only)
# Fetch last commit only (faster for large repos)
git fetch --depth=1 origin feature-branch
# Fetch last 10 commits
git fetch --depth=10 origin feature-branch
# Fetch all commits since specific date
git fetch --shallow-since=2024-01-01 origin feature-branch
Fetch and Create Local Branch
# Fetch remote branch and create local tracking branch
git fetch origin feature-branch
git checkout -b feature-branch origin/feature-branch
# Or use --track
git fetch origin feature-branch
git checkout --track origin/feature-branch
Fetch Specific Commit Range
# Fetch specific refspec
git fetch origin main:refs/remotes/origin/main
# Fetch only new commits
git fetch origin main --update-head-ok
View Fetched Branch Without Checkout
# Fetch branch
git fetch origin feature-branch
# View files without checking out
git ls-tree -r origin/feature-branch
# View diff between branches
git diff main..origin/feature-branch
# View log
git log origin/feature-branch
# View specific file content
git show origin/feature-branch:path/to/file.js
Fetch Branch and Merge
# Fetch specific branch
git fetch origin develop
# Merge into current branch
git merge origin/develop
# Or use rebase
git rebase origin/develop
Fetch Branch Tags
# Fetch branch with its tags
git fetch origin feature-branch --tags
# Fetch only tags for branch
git fetch origin 'refs/tags/*:refs/tags/*'
# Verify fetched tags
git tag -l
Fetch Deleted Remote Branches
# Fetch and remove local references to deleted remote branches
git fetch --prune origin
# Fetch specific branch and prune
git fetch --prune origin feature-branch
# See what would be pruned
git fetch --dry-run --prune origin
Force Fetch Branch
# Force fetch (overwrite local remote tracking)
git fetch --force origin feature-branch
# Useful when remote branch was force pushed
git fetch --force origin main:main
Configure Fetch Refspec
# View current fetch refspec
git config --get remote.origin.fetch
# Add custom refspec to fetch specific branches
git config --add remote.origin.fetch '+refs/heads/develop:refs/remotes/origin/develop'
# Fetch only main and develop 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'
Fetch with Progress
# Show progress during fetch
git fetch --progress origin feature-branch
# Verbose output
git fetch --verbose origin feature-branch
# Quiet mode
git fetch --quiet origin feature-branch
Automate Branch Fetch
Create .git/hooks/post-merge:
#!/bin/bash
# Automatically fetch specific branches after merge
git fetch origin develop
git fetch origin staging
echo "✓ Fetched latest develop and staging branches"
Make executable:
chmod +x .git/hooks/post-merge
Fetch Script for Multiple Branches
Create fetch-branches.sh:
#!/bin/bash
BRANCHES=("main" "develop" "staging" "feature-a" "feature-b")
echo "Fetching branches..."
for branch in "${BRANCHES[@]}"; do
echo "Fetching $branch..."
git fetch origin "$branch" || echo "⚠️ Failed to fetch $branch"
done
echo "✓ Fetch complete"
Usage:
chmod +x fetch-branches.sh
./fetch-branches.sh
Best Practice Note
This is how we optimize Git fetch operations across all CoreUI repositories for efficient synchronization. Fetching specific branches reduces bandwidth and time, especially in large repositories with many feature branches. Always use shallow fetches for CI/CD pipelines, prune deleted remote branches regularly, and configure refspecs to fetch only branches your team actively uses.
Related Articles
For related Git operations, check out how to track remote branch in Git and how to push specific branch in Git.



