Fatskills
Practice. Master. Repeat.
Study Guide: TECH **Git & GitHub Remotes: `origin`, `upstream`, `fetch`, `pull`, `push`**
Source: https://www.fatskills.com/web-development/chapter/tech-git-github-remotes-origin-upstream-fetch-pull-push

TECH **Git & GitHub Remotes: `origin`, `upstream`, `fetch`, `pull`, `push`**

By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.

⏱️ ~6 min read

Git & GitHub Remotes: origin, upstream, fetch, pull, push

A Hyper-Practical, Zero-Fluff Study Guide


1. What This Is & Why It Matters

You’re working on a team where multiple developers contribute to the same codebase. Your local copy of the repo is just one version—remotes (origin, upstream) are the bridges to other copies (e.g., GitHub, GitLab, or a colleague’s fork). Without understanding remotes, you’ll: - Break builds by pushing directly to the wrong branch.
- Lose work by overwriting others’ changes.
- Waste time resolving merge conflicts manually because you didn’t fetch first.

Real-world scenario:
You fork a company’s open-source project (upstream) to contribute a bug fix. You clone your fork (origin) locally. Before submitting a pull request, you need to: 1. Sync your local branch with upstream/main (to avoid conflicts).
2. Push your changes to origin/your-branch (so the team can review them).
3. Open a PR from origin/your-branchupstream/main.

Mess this up, and you’ll either: - Submit outdated code (failing CI checks).
- Accidentally push to upstream directly (breaking the main repo).

This guide teaches you the exact workflows to avoid these disasters.


2. Core Concepts & Components

  • remote
    A named reference to a remote repository (e.g., origin, upstream).
    Production insight: Always verify remotes with git remote -v before pushing. A misconfigured remote can send your code to the wrong repo.

  • origin
    The default remote name for the repository you cloned from (usually your fork).
    Production insight: Never push directly to origin/main in a shared project—use feature branches.

  • upstream
    The "source of truth" remote (e.g., the original repo you forked from).
    Production insight: If you don’t set upstream, you’ll miss critical updates from the main project.

  • git fetch
    Downloads changes from a remote but does not merge them into your local branches.
    Production insight: Always fetch before pull or merge to avoid surprises.

  • git pull
    Shortcut for git fetch + git merge (or git rebase if configured).
    Production insight: Prefer git fetch + manual merge/rebase in shared branches to avoid accidental overwrites.

  • git push
    Uploads your local commits to a remote branch.
    Production insight: Use --force-with-lease (not --force) to avoid overwriting others’ work.

  • Tracking Branches
    Local branches linked to remote branches (e.g., mainorigin/main).
    Production insight: If your branch isn’t tracking a remote, git push will fail with no upstream branch.

  • git remote prune
    Cleans up stale remote-tracking branches (e.g., deleted branches on GitHub).
    Production insight: Run this monthly to avoid clutter in git branch -a.


3. Step-by-Step Hands-On


Prerequisites



Task: Contribute to a Forked Repo (The Right Way)

  1. Fork the repo on GitHub
  2. Go to github.com/octocat/Hello-World → Click Fork (top-right).
  3. Now you have your-username/Hello-World (your origin).

  4. Clone your fork locally
    bash
    git clone https://github.com/your-username/Hello-World.git
    cd Hello-World
    git remote -v # Should show only 'origin'

  5. Add the original repo as upstream
    bash
    git remote add upstream https://github.com/octocat/Hello-World.git
    git remote -v # Now shows 'origin' and 'upstream'

  6. Sync your local main with upstream/main
    bash
    git fetch upstream # Download latest changes from upstream
    git checkout main # Switch to your local main branch
    git merge upstream/main # Merge upstream changes into your local main
    git push origin main # Update your fork on GitHub

  7. Create a feature branch and push it
    bash
    git checkout -b fix-typo # Create and switch to a new branch
    # Make changes (e.g., edit README.md)
    git add README.md
    git commit -m "Fix typo in README"
    git push -u origin fix-typo # Push to your fork and set upstream tracking

  8. Open a Pull Request (PR)

  9. Go to your fork on GitHub → Click Compare & pull request.
  10. Select your-username:fix-typooctocat:main.

Verification

  • Run git log --oneline --graph --all to see your branch diverged from upstream/main.
  • Check GitHub: Your PR should show "Able to merge" (no conflicts).


4. ? Production-Ready Best Practices


Security

  • Never hardcode credentials in remotes. Use SSH ([email protected]:user/repo.git) or GitHub CLI (gh auth login).
  • Use --force-with-lease instead of --force to avoid overwriting others’ commits.

Reliability

  • Always fetch before pull to inspect changes before merging.
  • Set upstream tracking for new branches (git push -u origin branch-name).
  • Use git pull --rebase in shared branches to avoid merge commits.

Maintainability

  • Prune stale branches monthly: bash git remote prune origin git branch -vv | grep ': gone]' | awk '{print $1}' | xargs git branch -D
  • Name remotes consistently (e.g., upstream for the original repo, origin for your fork).

Observability

  • Monitor remote changes with: bash git fetch --all --prune git log --oneline --graph --all
  • Use git status -sb for a concise overview of branch status.


5. ⚠️ Common Mistakes & Traps

Mistake Symptom Fix/Prevention
Pushing to upstream directly "Permission denied" or overwriting main repo. Always push to origin (your fork). Use git remote -v to verify.
Not setting upstream git pull fails with "no tracking information". Run git remote add upstream <url> and git branch -u upstream/main.
Using git pull blindly Unexpected merge conflicts. Use git fetch + git merge (or git rebase) instead.
Force-pushing (--force) Overwrites others’ commits. Use --force-with-lease and coordinate with the team.
Ignoring tracking branches git push fails with "no upstream branch". Run git push -u origin branch-name to set tracking.


6. ? Exam/Certification Focus


Typical Question Patterns

  1. "What’s the difference between git fetch and git pull?
  2. fetch downloads changes but doesn’t merge.
  3. pull = fetch + merge (or rebase).

  4. "How do you sync your fork with the original repo?"

  5. git remote add upstream <url>
  6. git fetch upstream
  7. git merge upstream/main

  8. "What does git push -u origin branch do?"

  9. Pushes the branch to origin and sets upstream tracking.

  10. "Why would git push fail with 'no upstream branch'?"

  11. The local branch isn’t tracking a remote branch. Fix with git push -u origin branch.

⚠️ Trap Distinctions

  • origin vs. upstream:
  • origin = your fork (where you push).
  • upstream = the original repo (where you pull updates).
  • --force vs. --force-with-lease:
  • --force overwrites remote history unconditionally.
  • --force-with-lease checks if the remote branch has new commits before overwriting.


7. ? Hands-On Challenge

Scenario:
You forked a repo and made a local commit. Meanwhile, the original repo (upstream) was updated. How do you sync your local branch with upstream/main without losing your commit?

Solution:


git fetch upstream
git rebase upstream/main  # Replay your commit on top of upstream changes
git push origin your-branch --force-with-lease

Why it works:
- fetch downloads the latest upstream changes.
- rebase moves your commit to the tip of upstream/main.
- --force-with-lease ensures you don’t overwrite others’ work.


8. ? Rapid-Reference Crib Sheet

Command Purpose
git remote -v List all remotes.
git remote add upstream <url> Add the original repo as upstream.
git fetch upstream Download changes from upstream (no merge).
git merge upstream/main Merge upstream/main into your current branch.
git pull --rebase Fetch + rebase (cleaner history).
git push -u origin branch Push branch to origin and set upstream tracking.
git push --force-with-lease Safely overwrite remote branch (checks for new commits first).
git remote prune origin Delete stale remote-tracking branches.
git branch -vv Show tracking branches.
git log --oneline --graph --all Visualize branch history.
⚠️ git push --force Dangerous! Overwrites remote history without checks.
⚠️ git pull Can cause unexpected merges. Prefer fetch + manual merge/rebase.


9. ? Where to Go Next

  1. GitHub Docs: About Remotes
  2. Git Official Docs: git-remote
  3. Atlassian Git Tutorial: Syncing
  4. GitHub CLI (gh) (for managing remotes via CLI).


ADVERTISEMENT