G
GuideDevOps
Lesson 5 of 18

Remote Repositories

Part of the Git & Version Control tutorial series.

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.git

Viewing 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 branches

Adding 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-repo

Remove Remote

git remote remove upstream

Rename Remote

git remote rename origin old-location

Change Remote URL

# Change from HTTPS to SSH
git remote set-url origin git@github.com:user/repo.git
 
# Change URL
git remote set-url origin https://github.com/new-url/repo.git

Fetching 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 --all

Fetch vs. Pull

CommandAction
git fetchDownload changes, don't merge
git pullFetch + 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 --all

Push 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 --tags

Force Push

# ⚠️ Dangerous - overwrites remote history
git push --force
 
# Slightly safer - only if fast-forward possible
git push --force-with-lease

Pulling from Remote

Pull Changes

# Fetch and merge
git pull
 
# Is equivalent to:
git fetch
git merge origin/main

Pull Specific Branch

git pull origin feature/search

Pull with Rebase

# Fetch and rebase instead of merge
git pull --rebase
 
# Keeps linear history

Tracking Branches

Set Upstream

# After first push of new branch
git push -u origin feature/auth
 
# Sets tracking: local feature/auth → origin/feature/auth

Check Tracking

git branch -vv
# Shows which remote each local branch tracks

Change Upstream

# Set upstream for existing branch
git branch --set-upstream-to=origin/main
 
# Track different remote
git branch --set-upstream-to=upstream/master

Working 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 repository

Syncing Fork with Upstream

Fetch Latest from Original

git fetch upstream

Rebase 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/main

Common 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.git

Contributing 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 UI

Multiple Remotes Setup

# Add personal backup
git remote add backup https://github.com/my-user/repo-backup.git
 
# Add staging server
git remote add staging git@staging-server.com:repo.git
 
# Push to all
git push --all
 
# Or specific
git push backup main
git push staging main

Cleanup

Remove Deleted Remote Branches

# Prune remote tracking branches
git fetch origin --prune
 
# Or during regular fetch
git fetch --prune

Remove Old Remote Branches Locally

git branch -vv | grep gone | awk '{print $1}' | xargs git branch -D

Tips

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