Home/Learn/Git/Working with Remotes — push, pull, fetch

Working with Remotes — push, pull, fetch

Beginner
Beginner

Remotes are named URLs to other copies of your repository. push sends your commits upstream; fetch downloads remote changes without merging; pull = fetch + merge. Understanding the difference between fetch and pull prevents nasty surprises.

Overview

A remote is just an alias for a URL. "origin" is the conventional name for the remote you cloned from. You can have multiple remotes (origin = your fork, upstream = the original project). Remote-tracking branches (origin/main) are local read-only snapshots of where the remote was last time you fetched — they update only when you run fetch or pull. Understanding this model — especially that origin/main is a snapshot, not a live view — resolves most confusion about why git pull is sometimes unsafe.

Remote Basics

Remotes are configured per-repository. You can add, rename, or remove them at any time. The remote URL can be HTTPS (username/password or token) or SSH (key-based, preferred for regular use).

Managing remotes
# View configured remotes
git remote -v
# origin  git@github.com:user/project.git (fetch)
# origin  git@github.com:user/project.git (push)

# Add a remote (e.g., upstream for open-source forks)
git remote add upstream https://github.com/original/project.git

# Rename/remove a remote
git remote rename origin old-origin
git remote remove upstream

# Change remote URL (e.g., switching HTTPS → SSH)
git remote set-url origin git@github.com:user/project.git

fetch vs pull — The Critical Difference

git fetch downloads remote changes into your remote-tracking branches (origin/main) but does NOT touch your working directory or local branches — it is always safe. git pull = git fetch + git merge (by default). This means pull can trigger a merge conflict or create an unwanted merge commit. Many teams prefer fetch + inspect + merge manually.

fetch vs pull explained
//  Remote-tracking branches — local snapshots of the remote
//
//  After git fetch:
//
//  Local:           origin/main (remote-tracking, read-only):
//  main  →  A──B   origin/main  →  A──B──C──D
//
//  Your local main has not moved — you can inspect what came in:
//  git log main..origin/main    # what's on remote but not local
//  git diff main origin/main    # what would change if you merged

# Fetch (always safe — never changes local branches)
git fetch origin
git fetch --all  # fetch all remotes

# Pull (fetch + merge — can cause merge commits)
git pull origin main

# Pull with rebase instead of merge (cleaner history)
git pull --rebase origin main

# Configure pull to always rebase
git config --global pull.rebase true

Pushing and Tracking

When you push a new branch for the first time, set the upstream tracking so future push/pull commands know which remote branch to use. After that, git push and git pull work without arguments on that branch.

Push and tracking branches
# Push and set upstream tracking (first push of a branch)
git push -u origin feature/payment-gateway
# Branch 'feature/payment-gateway' set up to track
# 'origin/feature/payment-gateway'.

# After -u is set, plain push/pull works
git push
git pull

# Push to a differently named remote branch
git push origin local-name:remote-name

# Delete a remote branch (after PR merged)
git push origin --delete feature/payment-gateway

# Force push (NEVER on shared branches — rewrites history)
git push --force-with-lease  # safer: fails if remote has new commits
git push --force             # dangerous — can lose others' work

Key Points to Remember

  • 1origin/main is a local snapshot of the remote — it only updates when you run fetch or pull
  • 2git fetch is always safe — it never modifies your working directory or local branches
  • 3git pull = git fetch + git merge — it can trigger merge commits or conflicts
  • 4git pull --rebase avoids merge commits — prefer this for keeping linear history
  • 5git push --force-with-lease is the safe way to force-push — it fails if the remote has new commits

Interview Questions

Sign in to ask Aria
1

What is the difference between git fetch and git pull?

Easy
2

What are remote-tracking branches? How do they differ from local branches?

Medium
3

When would you use git push --force-with-lease instead of --force?

Hard

Ask Aria about Working with Remotes — push, pull, fetch

Your personal AI tutor — ask anything about this concept

Revision Status

Personal Notes

Sign in to save personal notes for this topic.

Discussion

Sign in to join the discussion.

Loading discussion…