Skip to content

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.

Terminal window
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>)...]
CommandDescription
git remoteList all remote names
add <name> <URL>Add new remote repository
remove <name>Remove remote repository
rename <old> <new>Rename remote repository
CommandDescription
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
CommandDescription
set-head <name> <branch>Set default branch
set-branches <name> <branches>Configure tracked branches
prune <name>Remove stale branches
CommandDescription
show <name>Show remote details
update [<remote>]Fetch updates from remotes
-v, --verboseShow detailed output
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 repository
.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/main
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 tracking
Terminal window
# List all remotes
git remote
# List remotes with URLs
git remote -v
# Add new remote
git remote add origin https://github.com/user/repo.git
# Remove remote
git remote remove origin
# Rename remote
git remote rename origin upstream
Terminal window
# Get remote URL
git remote get-url origin
# Set new URL
git remote set-url origin https://github.com/user/repo.git
# Add additional push URL
git remote set-url --add --push origin git@github.com:user/repo.git
# Remove URL
git remote set-url --delete origin https://old-url.com/repo.git
Terminal window
# Set default branch for remote
git remote set-head origin main
# Configure tracked branches
git remote set-branches origin main develop
# Add more tracked branches
git remote set-branches --add origin feature-x
# Prune stale remote branches
git remote prune origin
Terminal window
# Configure multiple push URLs
git remote add origin https://github.com/user/repo.git
git remote set-url --add --push origin git@github.com:user/repo.git
git remote set-url --add --push origin https://gitlab.com/user/repo.git
# Result: Push to multiple repositories simultaneously
# Pull from primary URL only
Terminal window
# Create mirror remote
git remote add mirror https://backup.example.com/repo.git
# Configure for mirroring
git remote set-url --push mirror https://backup.example.com/repo.git
# Mirror push (push all refs)
git push --mirror mirror
Terminal window
# Set up fork workflow
git remote add upstream https://github.com/original/project.git
git remote add origin https://github.com/your-username/project.git
# Configure push to fork, pull from upstream
git config remote.pushDefault origin
git config branch.main.remote origin
git config branch.main.merge refs/heads/main
# Sync with upstream
git fetch upstream
git rebase upstream/main
Terminal window
# Enterprise remote configuration
git remote add enterprise https://git.company.com/project.git
# Configure authentication
git config credential.helper store
git config credential.https://git.company.com.username your-username
# Set up branch tracking
git remote set-branches enterprise main develop release
# Configure push options
git config remote.enterprise.push refs/heads/main:refs/heads/main
git config remote.enterprise.push refs/heads/develop:refs/for/review
Terminal window
# Configure default remote behavior
git config remote.pushDefault origin # Default push remote
git config remote.fetch.tags true # Fetch tags automatically
git config remote.origin.prune true # Prune during fetch
# Configure remote-specific settings
git config remote.origin.tagOpt --tags # Tag fetching options
git config remote.origin.fetch '+refs/heads/*:refs/remotes/origin/*'
# Configure URL rewriting
git config url."https://github.com/".insteadOf "gh:"
git config url."ssh://git@github.com/".pushInsteadOf "https://github.com/"
Terminal window
# Use HTTPS for public repositories
git remote set-url origin https://github.com/user/repo.git
# Use SSH for private repositories
git remote set-url origin git@github.com:user/repo.git
# Configure credential helpers securely
git config credential.helper /usr/local/bin/git-credential-manager
# Set up SSH keys properly
ssh-keygen -t ed25519 -C "your-email@example.com"
ssh-add ~/.ssh/id_ed25519
# Verify remote authenticity
ssh -T git@github.com
Terminal window
# Configure parallel fetching
git config fetch.parallel 4
git config submodule.fetchJobs 4
# Optimize connection reuse
git config http.maxRequests 100
git config http.minSessions 10
# Configure compression
git config core.compression 9
git config pack.compression 9
# Set up shallow cloning for large repos
git remote add shallow <url>
git config remote.shallow.shallow true
#!/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"
Terminal window
# Manage multiple related repositories
setup_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"
Terminal window
# Automated remote repository maintenance
automated_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_maintenance
Terminal window
# Diagnose connection issues
git ls-remote origin
# Check URL accessibility
curl -I $(git remote get-url origin)
# Test SSH connection
ssh -T $(git remote get-url origin | sed 's|.*@||' | cut -d: -f1)
# Debug HTTPS issues
GIT_CURL_VERBOSE=1 git fetch origin
Terminal window
# Clear stored credentials
git config --unset credential.helper
# Update personal access token
git remote set-url origin https://oauth-token@github.com/user/repo.git
# Check SSH key
ssh-add -l
ssh-keygen -lf ~/.ssh/id_rsa.pub
# Test authentication
ssh -T git@github.com
Terminal window
# List all remote branches
git branch -r
# Check remote branch status
git remote show origin
# Update remote branches
git fetch --all
# Prune deleted remote branches
git remote prune origin
# Reset remote tracking
git branch --set-upstream-to=origin/main main
Terminal window
# Check current URLs
git remote -v
# Fix malformed URLs
git remote set-url origin https://github.com/user/repo.git
# Handle URL with special characters
git remote set-url origin "https://user:pass@github.com/user/repo.git"
# Configure separate push/fetch URLs
git remote set-url origin https://github.com/user/repo.git
git remote set-url --push origin git@github.com:user/repo.git
Terminal window
# Force synchronization
git fetch --all --prune
# Reset remote tracking
git update-ref refs/remotes/origin/main origin/main
# Fix diverged branches
git reset --hard origin/main
# Clean up local branches
git branch --merged | grep -v main | xargs git branch -d
#!/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"
Terminal window
# Set up open source contribution workflow
setup_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"
Terminal window
# Set up disaster recovery with multiple remotes
setup_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/bash
echo "Performing automated backup..."
# Push to all remotes
git push origin --all --tags
git push backup --all --tags
git push mirror --mirror
# Verify backup integrity
for 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"
fi
done
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"
Terminal window
# Manage remotes in CI/CD pipelines
ci_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_management

What’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.

Use git remote set-url origin . For GitHub, update if you change username or repository name.

Yes, use git remote set-url —add to add multiple push URLs. Pull uses primary URL, push uses all configured URLs.

Use git remote remove . This deletes remote and all remote-tracking branches, but keeps local branches.

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.

Use git remote rename . Updates remote name and all remote-tracking branch references.

Yes, configure multiple push URLs for a remote. git push will send to all configured push URLs.

Use git remote show to see remote details, tracked branches, and push/pull URLs.

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 to push and set upstream tracking.

Yes, use git remote set-url for pull URL and git remote set-url —push for push URL.

Use git remote prune to remove local remote-tracking branches for deleted remote branches.

Deletes remote configuration and remote-tracking branches. Local branches remain but lose upstream tracking.

git remote add upstream and git remote add origin .

Yes, use git remote update or git fetch —all to fetch from all configured remotes.

Use git ls-remote to test connectivity and list available references.

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.

Use SSH keys, personal access tokens, or credential helpers. Configure per remote or globally.

  1. Repository Connection Management: Add, remove, and configure connections to remote repositories
  2. Multi-Repository Workflow: Manage relationships between forks, upstreams, and mirrors
  3. Collaboration Setup: Configure remotes for team collaboration and code sharing
  4. Backup and Redundancy: Set up multiple remotes for data redundancy and backup
  5. CI/CD Integration: Configure remotes for automated build and deployment pipelines
  6. Enterprise Repository Management: Manage complex remote configurations in large organizations