push Git Command Guide
The git push command updates remote refs using local refs, while sending objects necessary to complete the given refs. It publishes local commits to remote repositories and establishes tracking relationships between local and remote branches.
git push Syntax:
Section titled “git push Syntax:”git push [--all | --branches | --mirror | --tags] [--follow-tags] [--atomic] [-n | --dry-run] [--receive-pack=<git-receive-pack>] [--repo=<repository>] [-f | --force] [-d | --delete] [--prune] [-q | --quiet] [-v | --verbose] [-u | --set-upstream] [-o <string> | --push-option=<string>] [--[no-]signed|--signed=(true|false|if-asked)] [--force-with-lease[=<refname>[:<expect>]]] [--force-if-includes] [--no-verify] [<repository> [<refspec>...]]Push Target Options:
Section titled “Push Target Options:”| Option | Description |
|---|---|
--all | Push all branches |
--branches | Push all branches (same as —all) |
--mirror | Mirror all refs |
--tags | Push all tags |
--follow-tags | Push tags reachable from pushed branches |
Force Push Options:
Section titled “Force Push Options:”| Option | Description |
|---|---|
-f, --force | Force update remote refs |
--force-with-lease[=<refname>[:<expect>]] | Force with lease protection |
--force-if-includes | Force if remote includes specific commit |
Push Control Options:
Section titled “Push Control Options:”| Option | Description |
|---|---|
-n, --dry-run | Show what would be pushed |
--atomic | Use atomic transactions |
-d, --delete | Delete remote refs |
--prune | Remove remote branches not in local |
-u, --set-upstream | Set upstream tracking |
Authentication & Security Options:
Section titled “Authentication & Security Options:”| Option | Description |
|---|---|
| `—signed=(true | false |
--no-verify | Skip pre-push hooks |
-o, --push-option=<string> | Pass options to receive-pack |
Output Control Options:
Section titled “Output Control Options:”| Option | Description |
|---|---|
-q, --quiet | Suppress output |
-v, --verbose | Verbose output |
--progress | Show progress |
Parameters:
Section titled “Parameters:”| Parameter | Description |
|---|---|
<repository> | Remote repository name or URL |
<refspec> | Local and remote refs to push |
Understanding Push Operations:
Section titled “Understanding Push Operations:”Push Process Flow:
Section titled “Push Process Flow:”1. Validate: Check local commits and refs2. Negotiate: Determine what to send to remote3. Transfer: Send objects and update refs4. Verify: Confirm successful push5. Update: Set upstream tracking if requestedPush Strategies:
Section titled “Push Strategies:”Fast-forward push: Remote: A---B---C Local: A---B---C---D Result: A---B---C---D (both)
Force push (dangerous): Remote: A---B---C---X Local: A---B---C---D Result: A---B---C---D (overwrites X)Basic Push Examples:
Section titled “Basic Push Examples:”Standard Push Operations:
Section titled “Standard Push Operations:”# Push current branch to default remotegit push
# Push to specific remotegit push origin
# Push specific branchgit push origin main
# Push current branch and set upstreamgit push -u origin main
# Push all branchesgit push --all
# Push all tagsgit push --tagsBranch Management:
Section titled “Branch Management:”# Push new branchgit push -u origin feature-branch
# Delete remote branchgit push origin --delete old-branch
# Push multiple branchesgit push origin main develop
# Mirror repositorygit push --mirror originForce Push Operations:
Section titled “Force Push Operations:”# Force push (dangerous - overwrites remote)git push --force
# Force with lease (safer - checks remote state)git push --force-with-lease
# Force specific branchgit push --force-with-lease origin mainDry Run Operations:
Section titled “Dry Run Operations:”# Preview what would be pushedgit push --dry-run
# Verbose dry rungit push --dry-run --verbose
# Check what tags would be pushedgit push --tags --dry-runAdvanced Push Scenarios:
Section titled “Advanced Push Scenarios:”Upstream Tracking:
Section titled “Upstream Tracking:”# Set upstream for current branchgit push --set-upstream origin feature-x
# Push and set upstream in one commandgit push -u origin new-branch
# Change upstream remotegit branch --set-upstream-to=origin/main main
# Check upstream configurationgit branch -vvSigned Pushes:
Section titled “Signed Pushes:”# Sign all pushesgit push --signed
# Sign if remote supports itgit push --signed=if-asked
# Configure default signinggit config push.gpgSign true
# Use specific keygit config user.signingkey "your-key-id"Atomic Pushes:
Section titled “Atomic Pushes:”# Atomic push (all refs succeed or all fail)git push --atomic origin main develop
# Push multiple refs atomicallygit push --atomic --all
# Verify atomic behaviorgit push --atomic --dry-run --verbosePush Options:
Section titled “Push Options:”# Pass options to remote hooksgit push -o ci.skip
# Multiple push optionsgit push -o merge_request.create -o merge_request.target=main
# Configure default push optionsgit config remote.origin.pushOption "ci.skip"Configuration and Best Practices:
Section titled “Configuration and Best Practices:”Push Default Configuration:
Section titled “Push Default Configuration:”# Configure default push behaviorgit config push.default simple # Push current branchgit config push.default upstream # Push upstream branchgit config push.default current # Push current branch to same namegit config push.default matching # Push all matching branches
# Configure per-remotegit config remote.origin.push refs/heads/main:refs/heads/maingit config remote.origin.push refs/heads/develop:refs/heads/developRemote Configuration:
Section titled “Remote Configuration:”# Configure remote push URLgit remote set-url --push origin https://github.com/user/repo.git
# Separate push and fetch URLsgit remote set-url origin https://github.com/user/repo.gitgit remote set-url --push origin git@github.com:user/repo.git
# Configure push refspecsgit config remote.origin.push refs/heads/*:refs/heads/*Security Configuration:
Section titled “Security Configuration:”# Enable push signinggit config push.gpgSign truegit config commit.gpgSign true
# Configure credential helpersgit config credential.helper storegit config credential.helper cache
# Set up SSH keys for pushssh-keygen -t ed25519 -C "your-email@example.com"ssh-add ~/.ssh/id_ed25519Integration with Development Workflows:
Section titled “Integration with Development Workflows:”CI/CD Pipeline Integration:
Section titled “CI/CD Pipeline Integration:”#!/bin/bash# CI/CD push workflow
ci_push_workflow() { echo "=== CI Push Workflow ==="
# Configure Git for CI git config user.name "CI Bot" git config user.email "ci@example.com"
# Set remote URL with credentials git remote set-url origin "https://$CI_TOKEN@github.com/user/repo.git"
# Push with verification if git push --quiet --follow-tags; then echo "✓ Push successful"
# Verify push local remote_commit=$(git rev-parse HEAD) local remote_head=$(git ls-remote origin HEAD | awk '{print $1}')
if [ "$remote_commit" = "$remote_head" ]; then echo "✓ Push verified - commits match" return 0 else echo "✗ Push verification failed" return 1 fi else echo "✗ Push failed" return 1 fi}
ci_push_workflowRelease Management:
Section titled “Release Management:”# Release push workflowrelease_push() { local version="$1" local branch="${2:-main}"
echo "=== Release Push: v$version ==="
# Create and push release tag git tag -a "v$version" -m "Release v$version"
# Push branch and tags git push origin "$branch" git push origin "v$version"
# Create release branch if needed if [ "$branch" != "main" ] && [ "$branch" != "master" ]; then git push origin "$branch" fi
# Verify release if git ls-remote --tags origin | grep -q "v$version"; then echo "✓ Release v$version published successfully" else echo "✗ Release verification failed" return 1 fi}
release_push "2.1.0" "release/v2.1"Collaborative Development:
Section titled “Collaborative Development:”# Team push workflowteam_push_workflow() { local branch="${1:-$(git branch --show-current)}"
echo "=== Team Push Workflow: $branch ==="
# Check for unpushed commits local ahead_behind ahead_behind=$(git rev-list --left-right --count "HEAD...origin/$branch" 2>/dev/null || echo "0 0") local ahead=$(echo "$ahead_behind" | awk '{print $1}')
if [ "$ahead" -gt 0 ]; then echo "Found $ahead unpushed commits"
# Run pre-push checks if command -v pre-push-hook >/dev/null 2>&1; then if ! pre-push-hook; then echo "✗ Pre-push checks failed" return 1 fi fi
# Push with upstream tracking if git push --set-upstream origin "$branch"; then echo "✓ Branch $branch pushed successfully"
# Notify team (if notification system exists) # notify_team "Branch $branch updated with $ahead commits"
return 0 else echo "✗ Push failed" return 1 fi else echo "Branch $branch is already up to date" fi}
team_push_workflow "feature/dark-mode"Troubleshooting Common Push Issues:
Section titled “Troubleshooting Common Push Issues:”Authentication Failures:
Section titled “Authentication Failures:”# Check remote URLgit remote -v
# Test authenticationssh -T git@github.com 2>/dev/null && echo "SSH OK" || echo "SSH failed"
# Update credentialsgit config credential.helper store
# Use personal access tokengit remote set-url origin "https://oauth-token@github.com/user/repo.git"Non-Fast-Forward Rejections:
Section titled “Non-Fast-Forward Rejections:”# Check why push was rejectedgit status# Shows: Your branch is behind 'origin/main' by X commits
# Fetch and merge/rebase firstgit fetch origingit rebase origin/main # or git merge origin/main
# Then pushgit pushForce Push Considerations:
Section titled “Force Push Considerations:”# Safe force push with leasegit push --force-with-lease
# Check what would be overwrittengit log --oneline origin/main..HEAD
# Force push with confirmationread -p "Force push will overwrite remote commits. Continue? (y/N) " -n 1 -rif [[ $REPLY =~ ^[Yy]$ ]]; then git push --force-with-leasefiLarge Push Issues:
Section titled “Large Push Issues:”# Increase buffer sizegit config http.postBuffer 524288000 # 500MB
# Compress bettergit config core.compression 9git config pack.compression 9
# Use shallow push for large reposgit push --depth=1
# Split large pushesgit push origin main --no-thin # Disable delta compressionPermission Issues:
Section titled “Permission Issues:”# Check repository permissionscurl -s https://api.github.com/repos/user/repo | jq .permissions
# Verify branch protection rules# Check if force push is allowed# Verify required status checks
# Use different remote if neededgit remote add fork git@github.com:your-fork/repo.gitgit push fork mainHook Failures:
Section titled “Hook Failures:”# Skip hooks for urgent pushesgit push --no-verify
# Debug hook issuesGIT_TRACE=1 git push
# Check server-side hooks# Review pre-receive, update, post-receive hooksReal-World Usage Examples:
Section titled “Real-World Usage Examples:”Feature Branch Workflow:
Section titled “Feature Branch Workflow:”#!/bin/bash# Complete feature branch push workflow
feature_branch_push() { local feature_branch="$1" local target_branch="${2:-main}"
echo "=== Feature Branch Push: $feature_branch ==="
# Switch to feature branch git checkout "$feature_branch"
# Ensure branch is up to date with target git fetch origin git rebase "origin/$target_branch"
# Run tests before push if command -v run-tests >/dev/null 2>&1; then if ! run-tests; then echo "✗ Tests failed - not pushing" return 1 fi fi
# Push feature branch if git push --set-upstream origin "$feature_branch"; then echo "✓ Feature branch $feature_branch pushed"
# Create pull request hint echo "" echo "Next steps:" echo "1. Create pull request from $feature_branch to $target_branch" echo "2. Request code review" echo "3. Wait for CI checks to pass" echo "4. Merge when approved"
return 0 else echo "✗ Push failed" return 1 fi}
feature_branch_push "feature/user-authentication" "develop"Enterprise Deployment:
Section titled “Enterprise Deployment:”# Enterprise push with complianceenterprise_push() { local environment="$1" local version="$2"
echo "=== Enterprise Push: $environment v$version ==="
# Compliance checks if ! security_scan_passed "$version"; then echo "✗ Security scan failed" return 1 fi
if ! compliance_check_passed "$version"; then echo "✗ Compliance check failed" return 1 fi
# Select target remote based on environment case "$environment" in "staging") remote="staging" branch="staging" ;; "production") remote="production" branch="main" ;; *) echo "Unknown environment: $environment" return 1 ;; esac
# Tag release git tag -a "v$version-$environment" -m "Deploy v$version to $environment"
# Push with verification if git push "$remote" "$branch" --tags --signed; then echo "✓ Successfully deployed v$version to $environment"
# Log deployment log_deployment "$environment" "$version" "$(date)" "$USER"
# Trigger post-deployment tasks trigger_post_deploy "$environment" "$version"
return 0 else echo "✗ Deployment failed" return 1 fi}
enterprise_push "production" "2.1.0"Multi-Repository Synchronization:
Section titled “Multi-Repository Synchronization:”# Push to multiple related repositoriesmulti_repo_push() { local repos_file="$1" local branch="${2:-main}"
echo "=== Multi-Repository Push ==="
while IFS='|' read -r repo_path remote_url; do echo "Processing: $repo_path"
if [ -d "$repo_path/.git" ]; then cd "$repo_path"
# Set remote if needed if ! git remote | grep -q "^origin$"; then git remote add origin "$remote_url" fi
# Push branch if git push origin "$branch"; then echo "✓ $repo_path pushed successfully" else echo "✗ $repo_path push failed" fi
cd - >/dev/null else echo "Repository not found: $repo_path" fi done < "$repos_file"}
# Repository configuration file format:# /path/to/repo1|https://github.com/org/repo1.git# /path/to/repo2|https://github.com/org/repo2.git
multi_repo_push "repositories.txt" "main"What’s the difference between git push and git pull?
Section titled “What’s the difference between git push and git pull?”git push sends local commits to remote repository; git pull fetches remote commits and merges them locally. Push publishes work, pull synchronizes with others’ work.
How do I push a new branch to remote?
Section titled “How do I push a new branch to remote?”Use git push -u origin branch-name to push the branch and set upstream tracking. The -u flag establishes the tracking relationship for future pushes.
What does —force-with-lease do?
Section titled “What does —force-with-lease do?”Checks if remote refs haven’t changed since last fetch before force pushing. Safer than —force as it prevents overwriting unexpected remote changes.
How do I delete a remote branch?
Section titled “How do I delete a remote branch?”Use git push origin —delete branch-name to remove the branch from remote. Local branch remains until deleted with git branch -d.
Can I push to multiple remotes?
Section titled “Can I push to multiple remotes?”Yes, configure multiple remotes and push to each: git remote add upstream url, then git push origin && git push upstream.
What’s the impact of force push on collaborators?
Section titled “What’s the impact of force push on collaborators?”Force push rewrites remote history, causing issues for collaborators. They must rebase/reset their local branches. Avoid on shared branches.
How do I configure default push behavior?
Section titled “How do I configure default push behavior?”Set push.default=simple (Git 2.0+) to push only current branch to matching remote branch. Other options: upstream, current, matching.
What happens if push fails with non-fast-forward?
Section titled “What happens if push fails with non-fast-forward?”Remote has commits you don’t have locally. Fetch and merge/rebase first, then push. Or use —force-with-lease if you intend to overwrite.
Can I push without committing?
Section titled “Can I push without committing?”No, push sends committed objects. Uncommitted changes stay local. Use git stash to temporarily store uncommitted work.
How do I handle large pushes?
Section titled “How do I handle large pushes?”Increase http.postBuffer, use compression, disable delta for large files, or push in smaller batches. Consider using Git LFS for large files.
What’s the relationship between push and tracking branches?
Section titled “What’s the relationship between push and tracking branches?”Push updates remote refs. Tracking branches (—set-upstream/-u) establish relationship between local and remote branches for future operations.
Applications of the git push command
Section titled “Applications of the git push command”- Code Publishing: Share local commits with remote repositories and team members
- Branch Management: Create, update, and delete remote branches
- Release Deployment: Push tags and release branches to production environments
- Collaborative Development: Synchronize feature branches with team repositories
- CI/CD Integration: Automated pushing of build artifacts and deployment commits
- Repository Mirroring: Maintain synchronized copies across multiple remote locations