Git Notes
Learn how to add, remove, rename, and manage remote repositories in Git. Understand multiple remotes, upstream tracking, and remote URL configuration.
When you work with Git, your local repository is a completely independent copy of the project. It does not automatically know about any server or collaborator. Remote connections are what bridge the gap between your local machine and repositories hosted elsewhere. The git remote command manages these connections, letting you define where to push your work and where to pull updates from.
Think of remotes as bookmarks or nicknames for long URLs. Instead of typing https://github.com/your-organization/massive-enterprise-app.git every time you want to interact with the server, you simply say origin. It is a convenience mechanism with some important configuration behind it.
Understanding What Remotes Are
A remote is simply a named reference to another repository's URL. When you clone a repository, Git automatically creates a remote called origin pointing to the URL you cloned from:
| Local Repo | ◄───────► | origin (your GitHub fork) |
|---|---|---|
| - upstream | ◄───────► | upstream (original repo) |
| ◄───────► | staging (deploy server) |
You can have as many remotes as you need. Each points to a different repository and serves a different purpose.
Viewing Your Remotes
# List remote names
git remoteorigin upstream
# List with URLs (verbose mode)
git remote -vorigin https://github.com/yourname/project.git (fetch) origin https://github.com/yourname/project.git (push) upstream https://github.com/original-author/project.git (fetch) upstream https://github.com/original-author/project.git (push)
Notice that each remote has separate fetch and push URLs. They are usually the same, but Git allows them to be different for advanced setups like pushing to a mirror.
Adding a New Remote
The most common scenario is adding an upstream remote when working with forked repositories:
# Basic syntax
git remote add <name> <url>
# Add upstream for a forked project
git remote add upstream https://github.com/facebook/react.git
# Add a deployment remote
git remote add production git@deploy.company.com:app.git
# Add a colleague's repo for code sharing
git remote add alice https://github.com/alice/project.gitAfter adding a remote, you can fetch from it immediately:
git fetch upstream
# Now you have upstream/main, upstream/develop, etc.Removing and Renaming Remotes
# Remove a remote entirely (removes all tracking branches too)
git remote remove upstream
# or older syntax:
git remote rm upstream
# Rename a remote
git remote rename origin old-origin
git remote rename backup primaryWhen you rename a remote, all remote-tracking branches are updated automatically. So origin/main becomes old-origin/main.
Changing Remote URLs
This is extremely common when switching from HTTPS to SSH authentication, or when a repository moves to a new location:
# Switch from HTTPS to SSH (tired of entering passwords)
git remote set-url origin git@github.com:yourname/project.git
# Switch back to HTTPS
git remote set-url origin https://github.com/yourname/project.git
# Update after repository transfer
git remote set-url origin https://github.com/new-org/project.git
# Set different push URL (push to mirror, fetch from primary)
git remote set-url --push origin git@mirror.company.com:project.gitVerify the change worked:
git remote -vGetting Detailed Remote Information
git remote show origin* remote origin
Fetch URL: git@github.com:yourname/project.git
Push URL: git@github.com:yourname/project.git
HEAD branch: main
Remote branches:
develop tracked
main tracked
feature/auth tracked
feature/payments new (next fetch will store in remotes/origin)
Local branches configured for 'git pull':
develop merges with remote develop
main merges with remote main
Local refs configured for 'git push':
develop pushes to develop (up to date)
main pushes to main (fast-forwardable)This command reveals which branches are tracked, which are stale, and whether your local branches are ahead or behind their remote counterparts.
Working with Multiple Remotes
The fork workflow is the most common multi-remote setup:
# Clone your fork
git clone https://github.com/yourname/open-source-project.git
cd open-source-project
# Add the original repository as upstream
git remote add upstream https://github.com/original/open-source-project.git
# Sync workflow: get latest from upstream, push to your fork
git fetch upstream
git checkout main
git merge upstream/main
git push origin mainAnother scenario is deploying to multiple environments:
# Add deployment remotes
git remote add staging git@staging.heroku.com:myapp.git
git remote add production git@production.heroku.com:myapp.git
# Deploy to staging
git push staging main
# After testing, deploy to production
git push production mainPruning Stale References
Over time, remote branches get deleted (after PR merges), but your local tracking references stick around:
# Remove stale remote-tracking branches
git remote prune origin
# Or prune during fetch (recommended)
git fetch --prune origin
# Set auto-prune globally
git config --global fetch.prune true
# See which remote branches are stale
git remote show origin
# Look for "stale" entriesCommon Mistakes
Mistake 1: Forgetting to add upstream after forking. Without upstream, you cannot sync with the original project and your fork gradually becomes outdated.
Mistake 2: Pushing to wrong remote. When you have multiple remotes, always double-check which one you are pushing to, especially with production deployment remotes.
Mistake 3: Not pruning stale references. Your git branch -r list grows indefinitely with branches that no longer exist on the server, creating confusion about active work.
Interview Questions
Q1: What is the difference between origin and upstream?
These are conventional names, not requirements. origin typically refers to your personal remote repository (your fork or your GitHub copy). upstream refers to the original repository you forked from. You could name them anything, but this convention is universally understood.
Q2: How do you change a remote URL from HTTPS to SSH?
Use git remote set-url origin git@github.com:user/repo.git. This updates the URL in your local configuration without removing the remote or losing tracking relationships. Verify with git remote -v.
Q3: Can a local repository have multiple remotes and when is this useful?
Yes, you can have unlimited remotes. Common scenarios include fork workflows (origin + upstream), deployment to multiple environments (staging + production remotes), collaborating directly with a colleague (adding their repo as a remote), and maintaining mirrors.
Q4: What does git remote show origin display?
It shows fetch and push URLs, the HEAD branch, all remote branches with their tracking status (tracked, new, stale), which local branches are configured for pull, and which push to where, including whether they are up to date or behind.
Q5: How do you handle a remote repository that moved to a new URL?
Run git remote set-url origin <new-url> to update the URL. All existing branches, tracking relationships, and configuration remain intact. Only the URL changes.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Git Remote - Manage Remote Repository Connections.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Git & GitHub topic.
Search Terms
git-github, git & github, git, github, working, with, repositories, remote
Related Git & GitHub Topics