Adding a Remote
A remote is a named link to a repository hosted elsewhere, usually called origin.
Syntax
git remote add origin <url>A remote is a bookmark to a copy of your repo on another server, such as GitHub. The default name for your main remote is origin.
git remote add origin <url>— link a local repo to GitHub.git remote -v— list remotes and their URLs.git remote remove origin— unlink a remote.
You get the URL from the green Code button on the GitHub repo page.
Example
git remote add origin https://github.com/you/my-project.git
# Confirm it was added
git remote -v
# origin https://github.com/you/my-project.git (fetch)
# origin https://github.com/you/my-project.git (push)When to use it
- A developer links a local repo to GitHub after deciding to publish the project online.
- A contributor adds the original upstream repo as a second remote so they can pull in the maintainer's latest changes.
- A team switches hosting from GitLab to GitHub and updates the remote URL on every developer's machine.
More examples
Add GitHub as the origin remote
Registers GitHub as the remote named 'origin' and verifies both fetch and push URLs are set correctly.
git remote add origin https://github.com/username/my-project.git
# Verify
git remote -v
# origin https://github.com/username/my-project.git (fetch)
# origin https://github.com/username/my-project.git (push)Add upstream for a fork workflow
Adds a second remote called 'upstream' so you can pull the original maintainer's changes into your fork.
# origin points to your fork
git remote add upstream https://github.com/original-owner/project.git
git remote -v
# origin https://github.com/you/project.git (fetch)
# upstream https://github.com/original-owner/project.git (fetch)Change an existing remote URL
set-url replaces the URL on an existing remote, commonly used when switching authentication method from HTTPS to SSH.
# Update from HTTPS to SSH
git remote set-url origin [email protected]:username/my-project.git
git remote get-url origin
# [email protected]:username/my-project.git
Discussion