Remotes and GitHub
How to add a Git remote
Learn how to add a Git remote to your repository with git remote add origin, pointing it at your GitHub repo so you can push your code there.
An existing local repository does not automatically know where you want to publish it.
First, check whether a remote already exists:
git remote -v
If there is no remote named origin, add one:
git remote add origin git@github.com:flaviocopes/myrepo.git
This stores the URL under the name origin. It does not contact GitHub and it does not push any commits.
Verify the configuration:
git remote get-url origin
git remote -v
If Git says remote origin already exists, stop and inspect its URL. Do not delete it blindly. If the repository moved and you intend to change the existing URL, use:
git remote set-url origin git@github.com:flaviocopes/myrepo.git
After adding a remote, git status still shows the same working tree and local branch. Network state changes only when you fetch or push.
Exercise: add a remote to a disposable repository, inspect .git/config, then use git remote remove origin. Confirm that removing the configuration does not delete local commits.
Lesson completed