By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.
origin
upstream
fetch
pull
push
A Hyper-Practical, Zero-Fluff Study Guide
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-branch → upstream/main.
upstream/main
origin/your-branch
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.
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.
remote
git remote -v
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.
origin/main
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 fetch
merge
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 pull
git merge
git rebase
git push Uploads your local commits to a remote branch. Production insight: Use --force-with-lease (not --force) to avoid overwriting others’ work.
git push
--force-with-lease
--force
Tracking Branches Local branches linked to remote branches (e.g., main → origin/main). Production insight: If your branch isn’t tracking a remote, git push will fail with no upstream branch.
main
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.
git remote prune
git branch -a
git --version
Now you have your-username/Hello-World (your origin).
your-username/Hello-World
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'
bash git clone https://github.com/your-username/Hello-World.git cd Hello-World git remote -v # Should show only 'origin'
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'
bash git remote add upstream https://github.com/octocat/Hello-World.git git remote -v # Now shows 'origin' and 'upstream'
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
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
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
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
Open a Pull Request (PR)
your-username:fix-typo
octocat:main
git log --oneline --graph --all
[email protected]:user/repo.git
gh auth login
git push -u origin branch-name
git pull --rebase
bash git remote prune origin git branch -vv | grep ': gone]' | awk '{print $1}' | xargs git branch -D
bash git fetch --all --prune git log --oneline --graph --all
git status -sb
git remote add upstream <url>
git branch -u upstream/main
pull = fetch + merge (or rebase).
rebase
"How do you sync your fork with the original repo?"
git fetch upstream
git merge upstream/main
"What does git push -u origin branch do?"
git push -u origin branch
Pushes the branch to origin and sets upstream tracking.
"Why would git push fail with 'no upstream branch'?"
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.
git push --force-with-lease
git remote prune origin
git branch -vv
git push --force
git-remote
gh
Join 4M+ learners. Unlock unlimited quizzes, wrong-answer tracking, flashcards + reminders, study guides, and 1-on-1 challenges.