Work with Remote Repos on GitHub
Master working with remote repositories on GitHub — clone, push, pull, and manage remotes with hands-on steps, troubleshooting, and next steps.
Focus: work with remote repositories on github
You've spent hours crafting commits and branches locally, but the moment a teammate says “push it so I can pull it,” your terminal becomes a one-way ticket to confusion. Working with remote repositories on GitHub is where Git transforms from a personal time machine into a team superpower — and also where beginners hit the most frustrating walls: failed pushes, anonymous keys, and the dreaded "origin/master has diverged" error. This step-by-step lesson turns that pain into a clear workflow, so you can clone, push, pull, and manage remotes with confidence.
The problem this lesson solves
Local Git keeps your history safe, but it doesn't help anyone else. Without remotes, collaboration means zip files, email threads, and “the latest version” chaos. The real pain: you have no single source of truth, no backup off your machine, and no way to share your work with reviewers or CI pipelines. When you finally attempt your first push, you'll likely face protocol errors, merge conflicts after a pull, or the panic of realizing you pushed to the wrong branch.
This lesson addresses the most common remote-related problems you'll encounter as a developer:
- Failed authentication — Git refuses your credentials for HTTPS or SSH.
- Non-fast-forward errors — your push is rejected because the remote has commits you don't have.
- Orphaned or misconfigured remotes — you cloned from the wrong URL, or the remote name isn't
origin. - Branch tracking confusion — local and remote branches don't stay in sync.
By the end, you'll be able to identify each issue, understand why it happens, and resolve it with the right command — without randomly googling stack traces.
Core concept / mental model
Think of a remote repository as the team's shared whiteboard. Your local repository is your personal notebook. You write ideas (commits) in your notebook, then push them to the whiteboard so others can see. When teammates update the whiteboard, you pull those changes into your notebook. The whiteboard is the source of truth — indisputable and always available.
In Git terms:
- A remote is a URL to another copy of your repository, hosted on GitHub (or GitLab, Bitbucket, etc.).
- The default remote name is
origin, but you can have multiple remotes (e.g., a fork and the original upstream project). - Tracking is the link between a local branch and a remote branch — Git remembers which remote branch your local branch is based on.
Pro tip: A remote is not a separate repository concept — it's just an alias for a URL.
originis simply the conventional name, not a magic word.
Key commands we'll explore:
git clone— copies a remote repo to your machine and sets up tracking automatically.git remote -v— lists all remotes and their URLs.git push— uploads local commits to the remote.git pull— downloads remote commits and merges them into your current branch.git fetch— downloads remote commits without merging (safer for inspection).
How it works step by step
Here's the high-level flow of working with a remote on GitHub:
1. Create a remote repository on GitHub
- Log in to GitHub, click New repository, give it a name, and choose public or private.
- Do not initialize with a README if you want to push an existing local repo — this avoids merge conflicts.
2. Connect your local repo to the remote
If you already have a local repo, add the remote with git remote add origin <URL>. If you're starting fresh, git clone <URL> creates a local copy and sets up origin automatically.
3. Push your first commit
The first push to an empty remote requires git push -u origin main to establish the upstream (tracking) branch. After that, a simple git push works for that branch.
4. Pull changes from teammates
When others push, your local repo is behind. Run git pull to fetch and merge their commits into your working branch. If you have local commits that conflict, you'll resolve them before pushing your own work.
5. Repeat the cycle
The rhythm of collaboration: pull latest, make commits, push, repeat. For a solo project, you can skip pulling until you want to back up or deploy.
This sequence is the backbone of modern Git workflows — even in complex CI/CD setups, the fundamentals stay the same.
Hands-on walkthrough
Let's put theory into practice. We'll create a remote repo on GitHub, connect a local project, push, pull, and inspect remotes.
Example 1: Clone an existing remote repo
# Clone a public repo (replace with any URL)
git clone https://github.com/octocat/Hello-World.git
cd Hello-World
# List remotes and see origin
git remote -v
# Output:
# origin https://github.com/octocat/Hello-World.git (fetch)
# origin https://github.com/octocat/Hello-World.git (push)
Notice that git remote -v shows two lines for the same URL: one for fetch and one for push. They can be different if you use a read-only URL for pulling and a write URL for pushing.
Example 2: Add a remote to an existing local repo
# Assume you have a local repo with commits
git init my-project
cd my-project
echo "# My Project" > README.md
git add README.md
git commit -m "Initial commit"
# Now create an empty repo on GitHub, then:
git remote add origin https://github.com/yourusername/my-project.git
# Push and set upstream
git push -u origin main
# Output:
# Counting objects: 3, done.
# ...
# Branch 'main' set up to track 'origin/main'.
The -u (or --set-upstream) flag links your local main to origin/main, so future git push and git pull work without extra arguments.
Example 3: Pull and handle a conflict
# Ensure you're on main and up to date
git checkout main
git pull origin main
# If the remote has changes you don't, Git merges automatically (if no conflicts).
# If conflicts occur, you'll see:
# CONFLICT (content): Merge conflict in README.md
# Resolve manually, then:
git add README.md
git commit -m "Resolve merge conflict"
Conflict resolution is covered in a later lesson, but remember: after resolving, always commit the merge.
Example 4: Inspect remote branches
# List all remote branches
git branch -r
# Output:
# origin/HEAD -> origin/main
# origin/main
# Show all branches, local and remote
git branch -a
# Output:
# * main
# remotes/origin/main
This helps you see what's available on the remote before you create a local branch from it.
Compare options / when to choose what
When working with remotes, you often face two main decisions: which protocol to use and whether to use fetch + merge vs pull. Here's a comparison.
HTTPS vs SSH
| Option | Use case | Pros | Cons |
|---|---|---|---|
| HTTPS | Quick setup, corporate networks | Simpler auth (token), no key management | Need token/password on each push (unless cached) |
| SSH | Frequent pushes, personal machine | No password after key setup | Requires generating and adding SSH keys |
For most beginners, HTTPS with a personal access token is the easiest path. SSH is cleaner for daily work but involves extra setup.
git pull vs git fetch + git merge
| Command | What it does | When to use |
|---|---|---|
git pull |
Fetches and merges in one step | Quick updates when you don't need to review |
git fetch |
Downloads remote commits without merging | Inspect changes before integrating |
git fetch + git merge |
Explicit two-step update | Review remote changes first, or avoid automatic merge |
Pro tip: If you're working on a busy team, use
git fetchand inspect withgit log origin/mainbefore merging. It prevents surprise conflicts.
Multiple remotes: fork vs single remote
| Workflow | Best for | Example |
|---|---|---|
| Single remote (origin) | Small teams, direct collaborator | Team project on a shared repo |
| Fork + upstream | Open source contribution | Fork a project, add original as upstream |
Adding an upstream remote is common in open source:
git remote add upstream https://github.com/original/owner/repo.git
git fetch upstream
Then you can pull changes from upstream and push to your own fork.
Troubleshooting & edge cases
Here are the most common failures and how to fix them.
"Permission denied (publickey)" or "Username for 'https://github.com':"
- HTTPS: You need a personal access token instead of your password. Generate one on GitHub under Settings → Developer settings → Personal access tokens, then use it as your password when prompted.
- SSH: Your public key isn't added to GitHub. Check with
ssh -T git@github.com(you should see a welcome message). If not, add the key viassh-keygenand upload the.pubfile.
Push rejected: "failed to push some refs" or "non-fast-forward"
This means the remote has commits you don't have locally. Fix by pulling first:
git pull origin main
# Resolve conflicts if any
git push
Never use git push --force unless you're certain — it overwrites remote history and can destroy teammates' work.
Wrong remote URL
git remote set-url origin https://github.com/user/repo.git
Verify with git remote -v.
Remote branch not showing up after fetch
If you see origin/main in git branch -r but can't check it out:
git checkout -b local-main origin/main
This creates a local branch tracking the remote one.
What you learned & what's next
You've now got a solid handle on working with remote repositories on GitHub: you can clone, push, pull, add remotes, and troubleshoot common network and authentication issues. Specifically, you learned to:
- Explain the core idea of remotes as shared repositories with aliases like
origin. - Perform a full hands-on exercise: create a remote, connect a local repo, push, pull, and inspect branches.
- Choose between HTTPS and SSH, and between
pullandfetch+merge. - Resolve authentication errors, non-fast-forward pushes, and remote URL mistakes.
This foundation leads directly to the next lesson in the Git Tutorial track: branching and merging strategies. With remotes mastered, you'll be ready to explore feature branches, pull requests, and collaborative workflows that rely on the push/pull cycle you now own.
To cement your learning, try this recap: create a new GitHub repo, clone it, add a file, push it, then make a change on GitHub's web UI and pull it locally. That's the complete remote loop — and you've just mastered it.
Practice recap
Now that you've walked through the hands-on examples, run through the full loop yourself: create a new remote repository on GitHub, clone it, add a file, and push it. Then go to the GitHub web UI, edit the file, commit the change, and run git pull on your local machine. You've just practiced the essential remote workflow every developer uses daily — repeat it until it feels automatic.
Common mistakes
- Using
git push --forceto resolve a rejected push — this can overwrite teammates' commits and corrupt the remote history. Always pull and merge instead. - Forgetting the
-uflag on the first push — without it, futuregit pushcommands fail with 'no upstream branch' errors. Usegit push -u origin mainonce. - Caching your GitHub password in plain text (e.g., in
.git/configor viastorehelper) — use a credential manager or a token with limited scope instead. - Adding a remote that already exists — run
git remote -vfirst to check; otherwise you'll get a 'fatal: remote origin already exists' error.
Variations
- Use
git pull --rebaseinstead of the default merge to keep a linear history when pulling from a busy remote. - Add multiple remotes (e.g.,
originandupstream) when contributing to open source, so you can fetch from the original repo and push to your fork. - Switch between HTTPS and SSH with
git remote set-urlwhen security policies or credentials change.
Real-world use cases
- Contributing to an open-source project: clone the repo, add a feature branch, push to your fork, and submit a pull request to the upstream maintainers.
- Collaborating with a small team on a shared private repo: each developer pushes daily commits and pulls every morning to stay in sync with the latest changes.
- Automating deployments from a remote: set up a CI/CD pipeline that triggers on every
git pushtoorigin/mainto build and deploy your application.
Key takeaways
- A remote is just an alias for a URL —
originis the default name, but you can add and manage multiple remotes. - The core remote workflow is clone (or add), push, and pull — with
git fetchas a safe way to inspect remote changes before merging. - Authentication errors are usually HTTPS token issues or missing SSH keys — verify with
git remote -vandssh -T git@github.com. - If your push is rejected, pull and merge first — never force-push unless you absolutely know what you're doing.
- Tracking branches (
-u) are essential for streamed workflows; they let you use plaingit pushandgit pullwithout extra arguments.
Keep learning
Related tutorials, quizzes, and articles for this topic.
Discussion
Questions, corrections, and tips help everyone reading this page.
0 comments
Add a comment
No comments yet — start the thread.