What is a Remote?
A remote is a connection to a Git repository hosted elsewhere (GitHub, GitLab, Bitbucket, etc.).
The Default Remote
When you clone a repository, Git automatically creates a remote named origin:
git clone https://github.com/user/repo.git
# Automatically creates:
# remote "origin" → https://github.com/user/repo.gitViewing Remotes
List Remotes
git remote
# origin
# With URLs
git remote -v
# origin https://github.com/user/repo.git (fetch)
# origin https://github.com/user/repo.git (push)View Remote Details
git remote show origin
# Shows:
# - URL
# - Tracked branches
# - Default push/pull branchesAdding and Removing Remotes
Add Remote
# Add new remote
git remote add upstream https://github.com/original-author/repo.git
# Now you have multiple remotes
git remote -v
# origin my-fork
# upstream original-repoRemove Remote
git remote remove upstreamRename Remote
git remote rename origin old-locationChange Remote URL
# Change from HTTPS to SSH
git remote set-url origin [email protected]:user/repo.git
# Change URL
git remote set-url origin https://github.com/new-url/repo.gitFetching from Remote
Fetch Updates
# Download changes from remote (doesn't modify your branch)
git fetch
# Fetch from specific remote
git fetch origin
git fetch upstream
# Fetch all remotes
git fetch --allFetch vs. Pull
| Command | Action |
|---|---|
git fetch | Download changes, don't merge |
git pull | Fetch + merge in one step |
Pushing to Remote
Push Branch
# Push current branch
git push
# First time pushing new branch
git push -u origin feature/auth
# Sets upstream tracking
# Push specific branch
git push origin feature/auth
# Push all branches
git push --allPush Tags
# Push specific tag
git push origin v1.0.0
# Push all tags
git push origin --tags
# Push branch and tags
git push origin main --tagsForce Push
# ⚠️ Dangerous - overwrites remote history
git push --force
# Slightly safer - only if fast-forward possible
git push --force-with-leasePulling from Remote
Pull Changes
# Fetch and merge
git pull
# Is equivalent to:
git fetch
git merge origin/mainPull Specific Branch
git pull origin feature/searchPull with Rebase
# Fetch and rebase instead of merge
git pull --rebase
# Keeps linear historyTracking Branches
Set Upstream
# After first push of new branch
git push -u origin feature/auth
# Sets tracking: local feature/auth → origin/feature/authCheck Tracking
git branch -vv
# Shows which remote each local branch tracksChange Upstream
# Set upstream for existing branch
git branch --set-upstream-to=origin/main
# Track different remote
git branch --set-upstream-to=upstream/masterWorking with Forks
Fork Workflow
# 1. Fork on GitHub/GitLab (UI)
# 2. Clone your fork
git clone https://github.com/your-user/repo.git
# 3. Add upstream remote to original
git remote add upstream https://github.com/original-author/repo.git
# 4. Keep fork updated
git fetch upstream
git rebase upstream/main
# 5. Push to your fork
git push origin main
# 6. Create PR on original repositorySyncing Fork with Upstream
Fetch Latest from Original
git fetch upstreamRebase on Latest
# Update main
git checkout main
git rebase upstream/main
# Push to your fork
git push origin main
# Or merge instead of rebase
git merge upstream/mainCommon Workflows
Cloning Repository
git clone https://github.com/user/repo.git
# Clone into specific directory
git clone https://github.com/user/repo.git my-project
# Clone specific branch
git clone --branch release/v1.0 https://github.com/user/repo.git
# Shallow clone (faster, limited history)
git clone --depth 1 https://github.com/user/repo.gitContributing to Project
# Clone original
git clone https://github.com/project/repo.git
cd repo
# Create feature
git checkout -b fix/issue-123
git add fixed-file.ts
git commit -m "Fix issue #123"
# Push
git push -u origin fix/issue-123
# Create PR on GitHub UIMultiple Remotes Setup
# Add personal backup
git remote add backup https://github.com/my-user/repo-backup.git
# Add staging server
git remote add staging [email protected]:repo.git
# Push to all
git push --all
# Or specific
git push backup main
git push staging mainCleanup
Remove Deleted Remote Branches
# Prune remote tracking branches
git fetch origin --prune
# Or during regular fetch
git fetch --pruneRemove Old Remote Branches Locally
git branch -vv | grep gone | awk '{print $1}' | xargs git branch -DTips
✓ git fetch first to see what's new
✓ Use --set-upstream-to to track branches
✓ Add descriptive remote names (upstream, backup, etc.)
✓ Use --force-with-lease instead of --force
✓ Keep forks updated with upstream regularly
✓ Multiple remotes useful for backups/mirrors
✓ Always fetch before pushing to avoid conflicts