remote Git Command Guide
The git remote command manages the set of repositories (“remotes”) whose branches you track. It allows you to add, remove, rename, and configure remote repositories, as well as manage remote-tracking branches and URLs.
git remote Syntax:
Section titled “git remote Syntax:”git remote [-v | --verbose]git remote add [-t <branch>] [-m <master>] [-f] [--[no-]tags] [--mirror=(fetch|push)] <name> <URL>git remote rename [--[no-]progress] <old> <new>git remote remove <name>git remote set-head <name> (-a | --auto | -d | --delete | <branch>)git remote set-branches [--add] <name> <branch>...git remote get-url [--push] [--all] <name>git remote set-url [--push] <name> <newurl> [<oldurl>]git remote set-url --add [--push] <name> <newurl>git remote set-url --delete [--push] <name> <URL>git remote [-v | --verbose] show [-n] <name>...git remote prune [-n | --dry-run] <name>...git remote [-v | --verbose] update [-p | --prune] [(<group> | <remote>)...]Core Operations:
Section titled “Core Operations:”Repository Management:
Section titled “Repository Management:”| Command | Description |
|---|---|
git remote | List all remote names |
add <name> <URL> | Add new remote repository |
remove <name> | Remove remote repository |
rename <old> <new> | Rename remote repository |
URL Management:
Section titled “URL Management:”| Command | Description |
|---|---|
get-url <name> | Get remote URLs |
set-url <name> <url> | Set remote URL |
set-url --add <name> <url> | Add additional URL |
set-url --delete <name> <url> | Remove URL |
Branch Management:
Section titled “Branch Management:”| Command | Description |
|---|---|
set-head <name> <branch> | Set default branch |
set-branches <name> <branches> | Configure tracked branches |
prune <name> | Remove stale branches |
Information & Updates:
Section titled “Information & Updates:”| Command | Description |
|---|---|
show <name> | Show remote details |
update [<remote>] | Fetch updates from remotes |
-v, --verbose | Show detailed output |
Understanding Remote Repositories:
Section titled “Understanding Remote Repositories:”Remote Repository Types:
Section titled “Remote Repository Types:”Origin Remote:├── Primary remote repository├── Usually where you cloned from├── Default push/pull destination└── Named "origin" by convention
Upstream Remote:├── Remote for forked repositories├── Points to original project├── Used for syncing changes└── Often named "upstream"
Multiple Remotes:├── origin: Main development repo├── upstream: Original project repo├── fork: Your personal fork└── backup: Backup repositoryRemote Configuration Structure:
Section titled “Remote Configuration Structure:”.git/config Remote Configuration:[remote "origin"] url = https://github.com/user/repo.git fetch = +refs/heads/*:refs/remotes/origin/* pushurl = git@github.com:user/repo.git
[branch "main"] remote = origin merge = refs/heads/mainRemote Tracking Branches:
Section titled “Remote Tracking Branches:”Remote Tracking Branches:├── Local copies of remote branches├── Located in refs/remotes/<remote>/├── Updated by git fetch/git pull├── Read-only (don't commit directly)└── Basis for local branch trackingBasic Remote Operations:
Section titled “Basic Remote Operations:”Managing Remotes:
Section titled “Managing Remotes:”# List all remotesgit remote
# List remotes with URLsgit remote -v
# Add new remotegit remote add origin https://github.com/user/repo.git
# Remove remotegit remote remove origin
# Rename remotegit remote rename origin upstreamURL Management:
Section titled “URL Management:”# Get remote URLgit remote get-url origin
# Set new URLgit remote set-url origin https://github.com/user/repo.git
# Add additional push URLgit remote set-url --add --push origin git@github.com:user/repo.git
# Remove URLgit remote set-url --delete origin https://old-url.com/repo.gitBranch Management:
Section titled “Branch Management:”# Set default branch for remotegit remote set-head origin main
# Configure tracked branchesgit remote set-branches origin main develop
# Add more tracked branchesgit remote set-branches --add origin feature-x
# Prune stale remote branchesgit remote prune originAdvanced Remote Configurations:
Section titled “Advanced Remote Configurations:”Multi-URL Setup:
Section titled “Multi-URL Setup:”# Configure multiple push URLsgit remote add origin https://github.com/user/repo.gitgit remote set-url --add --push origin git@github.com:user/repo.gitgit remote set-url --add --push origin https://gitlab.com/user/repo.git
# Result: Push to multiple repositories simultaneously# Pull from primary URL onlyMirror Repository Setup:
Section titled “Mirror Repository Setup:”# Create mirror remotegit remote add mirror https://backup.example.com/repo.git
# Configure for mirroringgit remote set-url --push mirror https://backup.example.com/repo.git
# Mirror push (push all refs)git push --mirror mirrorFork Workflow Setup:
Section titled “Fork Workflow Setup:”# Set up fork workflowgit remote add upstream https://github.com/original/project.gitgit remote add origin https://github.com/your-username/project.git
# Configure push to fork, pull from upstreamgit config remote.pushDefault origingit config branch.main.remote origingit config branch.main.merge refs/heads/main
# Sync with upstreamgit fetch upstreamgit rebase upstream/mainEnterprise Remote Management:
Section titled “Enterprise Remote Management:”# Enterprise remote configurationgit remote add enterprise https://git.company.com/project.git
# Configure authenticationgit config credential.helper storegit config credential.https://git.company.com.username your-username
# Set up branch trackinggit remote set-branches enterprise main develop release
# Configure push optionsgit config remote.enterprise.push refs/heads/main:refs/heads/maingit config remote.enterprise.push refs/heads/develop:refs/for/reviewConfiguration and Best Practices:
Section titled “Configuration and Best Practices:”Git Configuration for Remotes:
Section titled “Git Configuration for Remotes:”# Configure default remote behaviorgit config remote.pushDefault origin # Default push remotegit config remote.fetch.tags true # Fetch tags automaticallygit config remote.origin.prune true # Prune during fetch
# Configure remote-specific settingsgit config remote.origin.tagOpt --tags # Tag fetching optionsgit config remote.origin.fetch '+refs/heads/*:refs/remotes/origin/*'
# Configure URL rewritinggit config url."https://github.com/".insteadOf "gh:"git config url."ssh://git@github.com/".pushInsteadOf "https://github.com/"Remote Security Best Practices:
Section titled “Remote Security Best Practices:”# Use HTTPS for public repositoriesgit remote set-url origin https://github.com/user/repo.git
# Use SSH for private repositoriesgit remote set-url origin git@github.com:user/repo.git
# Configure credential helpers securelygit config credential.helper /usr/local/bin/git-credential-manager
# Set up SSH keys properlyssh-keygen -t ed25519 -C "your-email@example.com"ssh-add ~/.ssh/id_ed25519
# Verify remote authenticityssh -T git@github.comPerformance Optimization:
Section titled “Performance Optimization:”# Configure parallel fetchinggit config fetch.parallel 4git config submodule.fetchJobs 4
# Optimize connection reusegit config http.maxRequests 100git config http.minSessions 10
# Configure compressiongit config core.compression 9git config pack.compression 9
# Set up shallow cloning for large reposgit remote add shallow <url>git config remote.shallow.shallow trueIntegration with Development Workflows:
Section titled “Integration with Development Workflows:”Collaborative Development Setup:
Section titled “Collaborative Development Setup:”#!/bin/bash# Set up collaborative development environment
setup_collaborative_workflow() { local project_url="$1" local fork_url="$2"
# Add upstream remote git remote add upstream "$project_url"
# Add origin remote (fork) git remote add origin "$fork_url"
# Configure branch tracking git config branch.main.remote origin git config branch.main.merge refs/heads/main
# Set up tracking for development branches git checkout -b develop git branch --set-upstream-to=origin/develop develop
# Configure push default git config remote.pushDefault origin
echo "Collaborative workflow configured" echo "Pull from upstream, push to origin"}
setup_collaborative_workflow "https://github.com/org/project.git" "https://github.com/user/project.git"Multi-Repository Management:
Section titled “Multi-Repository Management:”# Manage multiple related repositoriessetup_multi_repo_workflow() { local base_org="$1" local repos=("api" "web" "mobile" "docs")
for repo in "${repos[@]}"; do echo "Setting up $repo..."
# Clone repository git clone "https://github.com/$base_org/$repo.git" cd "$repo"
# Add common remotes git remote add upstream "https://github.com/upstream-org/$repo.git" git remote add staging "https://github.com/$base_org/$repo-staging.git"
# Configure tracking git config remote.pushDefault origin
cd .. done
echo "Multi-repository setup complete"}
setup_multi_repo_workflow "my-org"Automated Remote Maintenance:
Section titled “Automated Remote Maintenance:”# Automated remote repository maintenanceautomated_remote_maintenance() { echo "Performing automated remote maintenance..."
# Update all remotes git remote update --prune
# Check for remote changes for remote in $(git remote); do echo "Checking $remote..."
# Get remote status local ahead_behind ahead_behind=$(git rev-list --left-right --count "HEAD...$remote/main" 2>/dev/null || echo "0 0") local ahead=$(echo "$ahead_behind" | awk '{print $1}') local behind=$(echo "$ahead_behind" | awk '{print $2}')
if [ "$behind" -gt 0 ]; then echo " $remote has $behind new commits" fi
if [ "$ahead" -gt 0 ]; then echo " Local is $ahead commits ahead of $remote" fi done
# Clean up stale branches git remote prune origin
echo "Remote maintenance complete"}
automated_remote_maintenanceTroubleshooting Common Issues:
Section titled “Troubleshooting Common Issues:”Remote Connection Problems:
Section titled “Remote Connection Problems:”# Diagnose connection issuesgit ls-remote origin
# Check URL accessibilitycurl -I $(git remote get-url origin)
# Test SSH connectionssh -T $(git remote get-url origin | sed 's|.*@||' | cut -d: -f1)
# Debug HTTPS issuesGIT_CURL_VERBOSE=1 git fetch originAuthentication Failures:
Section titled “Authentication Failures:”# Clear stored credentialsgit config --unset credential.helper
# Update personal access tokengit remote set-url origin https://oauth-token@github.com/user/repo.git
# Check SSH keyssh-add -lssh-keygen -lf ~/.ssh/id_rsa.pub
# Test authenticationssh -T git@github.comRemote Branch Issues:
Section titled “Remote Branch Issues:”# List all remote branchesgit branch -r
# Check remote branch statusgit remote show origin
# Update remote branchesgit fetch --all
# Prune deleted remote branchesgit remote prune origin
# Reset remote trackinggit branch --set-upstream-to=origin/main mainURL Configuration Problems:
Section titled “URL Configuration Problems:”# Check current URLsgit remote -v
# Fix malformed URLsgit remote set-url origin https://github.com/user/repo.git
# Handle URL with special charactersgit remote set-url origin "https://user:pass@github.com/user/repo.git"
# Configure separate push/fetch URLsgit remote set-url origin https://github.com/user/repo.gitgit remote set-url --push origin git@github.com:user/repo.gitSynchronization Issues:
Section titled “Synchronization Issues:”# Force synchronizationgit fetch --all --prune
# Reset remote trackinggit update-ref refs/remotes/origin/main origin/main
# Fix diverged branchesgit reset --hard origin/main
# Clean up local branchesgit branch --merged | grep -v main | xargs git branch -dReal-World Usage Examples:
Section titled “Real-World Usage Examples:”Enterprise Repository Migration:
Section titled “Enterprise Repository Migration:”#!/bin/bash# Migrate repositories between Git hosting services
enterprise_migration() { local old_domain="$1" local new_domain="$2" local org="$3"
echo "Migrating from $old_domain to $new_domain for org $org"
# Find all repositories repos=$(curl -s "https://api.$old_domain/orgs/$org/repos" | jq -r '.[].name')
for repo in $repos; do echo "Migrating $repo..."
# Clone from old location git clone "https://$old_domain/$org/$repo.git" "$repo-migrate" cd "$repo-migrate"
# Update remote URL git remote set-url origin "https://$new_domain/$org/$repo.git"
# Push to new location git push --mirror origin
# Update any submodules git submodule foreach 'git remote set-url origin $(echo $(git config --get remote.origin.url) | sed "s/$old_domain/$new_domain/")'
# Verify migration git remote -v git log --oneline -5
cd .. rm -rf "$repo-migrate"
echo "✓ $repo migrated successfully" done
echo "Enterprise migration complete"}
enterprise_migration "old-git.company.com" "new-git.company.com" "engineering"Open Source Contribution Workflow:
Section titled “Open Source Contribution Workflow:”# Set up open source contribution workflowsetup_open_source_contribution() { local upstream_url="$1" local fork_url="$2"
echo "Setting up open source contribution workflow"
# Add upstream remote git remote add upstream "$upstream_url"
# Set origin to fork git remote add origin "$fork_url"
# Configure pull from upstream, push to origin git config remote.pushDefault origin git config branch.main.remote origin git config branch.main.merge refs/heads/main
# Create contribution branches git checkout -b feature/contribution git checkout -b bugfix/issue-123
# Set up tracking for contribution branches git branch --set-upstream-to=origin/feature/contribution feature/contribution
# Configure pull to merge from upstream git config branch.main.mergeOptions "--no-ff"
echo "Open source workflow configured" echo "Workflow:" echo " 1. git checkout -b feature/new-feature" echo " 2. Make changes and commit" echo " 3. git fetch upstream && git rebase upstream/main" echo " 4. git push origin feature/new-feature" echo " 5. Create pull request"}
setup_open_source_contribution "https://github.com/project/project.git" "https://github.com/user/project.git"Disaster Recovery with Multiple Remotes:
Section titled “Disaster Recovery with Multiple Remotes:”# Set up disaster recovery with multiple remotessetup_disaster_recovery() { local primary_url="$1" local backup_url="$2" local mirror_url="$3"
echo "Setting up disaster recovery configuration"
# Configure primary remote git remote add origin "$primary_url"
# Configure backup remote git remote add backup "$backup_url"
# Configure mirror remote git remote add mirror "$mirror_url"
# Set up multi-push configuration git remote set-url --add --push origin "$backup_url" git remote set-url --add --push origin "$mirror_url"
# Configure pull from primary only git config remote.origin.url "$primary_url"
# Set up automated backup cat > backup-script.sh << 'EOF'#!/bin/bashecho "Performing automated backup..."
# Push to all remotesgit push origin --all --tagsgit push backup --all --tagsgit push mirror --mirror
# Verify backup integrityfor remote in origin backup mirror; do if git ls-remote "$remote" >/dev/null 2>&1; then echo "✓ $remote backup successful" else echo "✗ $remote backup failed" fidone
echo "Backup process complete"EOF
chmod +x backup-script.sh
# Schedule automated backups (crontab -l ; echo "0 */6 * * * $(pwd)/backup-script.sh") | crontab -
echo "Disaster recovery setup complete" echo "Data is automatically backed up to multiple locations"}
setup_disaster_recovery "https://primary.git.com/repo.git" "https://backup.git.com/repo.git" "https://mirror.git.com/repo.git"CI/CD Remote Management:
Section titled “CI/CD Remote Management:”# Manage remotes in CI/CD pipelinesci_remote_management() { echo "CI/CD remote management..."
# Configure remotes for CI git remote add ci-origin "https://$CI_TOKEN@github.com/org/repo.git" git remote add staging "https://$STAGING_TOKEN@staging.git.com/repo.git"
# Set up branch tracking git branch --set-upstream-to="origin/$CI_COMMIT_REF_NAME" "$CI_COMMIT_REF_NAME"
# Configure push destinations if [ "$CI_COMMIT_REF_NAME" = "main" ]; then # Push to production and staging git remote set-url --add --push ci-origin "https://$PROD_TOKEN@prod.git.com/repo.git" git push ci-origin "$CI_COMMIT_REF_NAME" elif [ "$CI_COMMIT_REF_NAME" = "develop" ]; then # Push to staging only git push staging "$CI_COMMIT_REF_NAME" else # Push feature branches to origin git push ci-origin "$CI_COMMIT_REF_NAME" fi
# Clean up CI remotes git remote remove ci-origin git remote remove staging
echo "CI/CD remote management complete"}
ci_remote_managementWhat’s the difference between origin and upstream?
Section titled “What’s the difference between origin and upstream?”Origin is where you cloned from (your fork); upstream is the original project repository. Pull from upstream to stay updated, push to origin for your changes.
How do I change a remote URL?
Section titled “How do I change a remote URL?”Use git remote set-url origin
Can I have multiple URLs for one remote?
Section titled “Can I have multiple URLs for one remote?”Yes, use git remote set-url —add to add multiple push URLs. Pull uses primary URL, push uses all configured URLs.
How do I remove a remote?
Section titled “How do I remove a remote?”Use git remote remove
What’s the difference between fetch and pull?
Section titled “What’s the difference between fetch and pull?”git fetch downloads changes from remote; git pull does fetch plus merge/rebase. fetch is safer for reviewing changes first.
How do I rename a remote?
Section titled “How do I rename a remote?”Use git remote rename
Can I push to multiple remotes at once?
Section titled “Can I push to multiple remotes at once?”Yes, configure multiple push URLs for a remote. git push will send to all configured push URLs.
How do I see detailed remote information?
Section titled “How do I see detailed remote information?”Use git remote show
What’s a remote-tracking branch?
Section titled “What’s a remote-tracking branch?”Local copy of remote branch, updated by fetch/pull. Located in refs/remotes/, used for comparing local vs remote state.
How do I set up tracking for a new branch?
Section titled “How do I set up tracking for a new branch?”After creating branch, use git push -u origin
Can I have different push and pull URLs?
Section titled “Can I have different push and pull URLs?”Yes, use git remote set-url for pull URL and git remote set-url —push for push URL.
How do I clean up old remote branches?
Section titled “How do I clean up old remote branches?”Use git remote prune
What’s the impact of removing a remote?
Section titled “What’s the impact of removing a remote?”Deletes remote configuration and remote-tracking branches. Local branches remain but lose upstream tracking.
How do I add a remote for a fork?
Section titled “How do I add a remote for a fork?”git remote add upstream
Can I fetch from all remotes at once?
Section titled “Can I fetch from all remotes at once?”Yes, use git remote update or git fetch —all to fetch from all configured remotes.
How do I check if a remote is accessible?
Section titled “How do I check if a remote is accessible?”Use git ls-remote
What’s the difference between mirror and regular remote?
Section titled “What’s the difference between mirror and regular remote?”Mirror remote replicates entire repository including all refs; regular remote tracks specific branches.
How do I handle remote authentication?
Section titled “How do I handle remote authentication?”Use SSH keys, personal access tokens, or credential helpers. Configure per remote or globally.
Applications of the git remote command
Section titled “Applications of the git remote command”- Repository Connection Management: Add, remove, and configure connections to remote repositories
- Multi-Repository Workflow: Manage relationships between forks, upstreams, and mirrors
- Collaboration Setup: Configure remotes for team collaboration and code sharing
- Backup and Redundancy: Set up multiple remotes for data redundancy and backup
- CI/CD Integration: Configure remotes for automated build and deployment pipelines
- Enterprise Repository Management: Manage complex remote configurations in large organizations