How to push specific branch in Git
Pushing a specific branch in Git uploads your local commits to the remote repository for that branch. As the creator of CoreUI with 26 years of development experience, I’ve managed Git workflows across hundreds of repositories, using targeted branch pushes to maintain clean deployment pipelines and prevent accidental updates to protected branches.
The fastest way is using git push with the branch name specified.
Push Current Branch
# Push current branch to origin
git push origin feature-branch
# If tracking is set up
git push
Push and Set Upstream
# Push and set tracking in one command
git push -u origin feature-branch
# Now future pushes can use
git push
Push Different Local to Remote Branch
# Push local branch to different remote branch name
git push origin local-branch:remote-branch
# Example
git push origin feature:feature-v2
Push All Branches
# Push all local branches
git push --all origin
# Push all branches and tags
git push --all origin
git push --tags origin
# Or combined
git push --all --follow-tags origin
Force Push Specific Branch
# Force push (overwrite remote)
git push --force origin feature-branch
# Safer force push (fails if remote has commits you don't have)
git push --force-with-lease origin feature-branch
Push to Multiple Remotes
# Add multiple remotes
git remote add origin https://github.com/user/repo.git
git remote add backup https://gitlab.com/user/repo.git
# Push to specific remote
git push origin feature-branch
git push backup feature-branch
# Or push to all remotes
git remote | xargs -L1 git push --all
Push with Tags
# Push branch with its tags
git push origin feature-branch --tags
# Push specific tag
git push origin v1.0.0
# Push all tags
git push origin --tags
Delete Remote Branch
# Delete remote branch
git push origin --delete feature-branch
# Or alternative syntax
git push origin :feature-branch
Push Only if Fast-Forward
# Push only if no conflicts (fast-forward only)
git push --ff-only origin feature-branch
# Fails if remote has commits you don't have
Push Dry Run
# See what would be pushed without actually pushing
git push --dry-run origin feature-branch
# Output shows refs that would be updated
Push with Lease (Safe Force Push)
# Safer than --force, checks remote state first
git push --force-with-lease origin feature-branch
# With specific expected remote state
git push --force-with-lease=feature-branch:abc1234 origin feature-branch
Configure Default Push Behavior
# Push only current branch
git config --global push.default simple
# Push all matching branches
git config --global push.default matching
# Push current branch and set upstream
git config --global push.default upstream
# Automatically set upstream on first push
git config --global push.autoSetupRemote true
Push Script for Multiple Branches
Create push-branches.sh:
#!/bin/bash
BRANCHES=("main" "develop" "staging")
echo "Pushing branches to origin..."
for branch in "${BRANCHES[@]}"; do
echo "Pushing $branch..."
git push origin "$branch" || {
echo "⚠️ Failed to push $branch"
continue
}
echo "✓ Pushed $branch"
done
echo "✓ All branches pushed"
Usage:
chmod +x push-branches.sh
./push-branches.sh
Push with Hooks
Create .git/hooks/pre-push:
#!/bin/bash
# Get current branch
BRANCH=$(git rev-parse --abbrev-ref HEAD)
# Prevent accidental push to main
if [ "$BRANCH" = "main" ]; then
echo "❌ Cannot push directly to main branch"
echo "Please create a pull request instead"
exit 1
fi
# Run tests before push
npm test || {
echo "❌ Tests failed, push aborted"
exit 1
}
exit 0
Make executable:
chmod +x .git/hooks/pre-push
Push Options
# Verbose output
git push --verbose origin feature-branch
# Quiet mode (no output unless error)
git push --quiet origin feature-branch
# Push with progress
git push --progress origin feature-branch
# No verify (skip pre-push hook)
git push --no-verify origin feature-branch
Best Practice Note
This is how we manage branch pushes across all CoreUI repositories for controlled deployments. Always use --force-with-lease instead of --force to prevent overwriting others’ work, set up tracking with -u on first push to simplify future pushes, and configure pre-push hooks to prevent accidental pushes to protected branches. Use --dry-run to preview changes before pushing to production branches.
Related Articles
For related Git operations, check out how to track remote branch in Git and how to fetch specific branch in Git.



