If you originally obtained your project with:
git clonethen the remote repository is usually already configured.
However, if you created the project locally with:
git initGit does not yet know which GitHub repository it should connect to. In that case, you need to add the remote manually.
Creating a Repository on GitHub
Suppose you already have a local project called:
git-demoand it has already been initialized with:
git initYou can now create a new repository on GitHub with the same or a similar name.
GitHub will give you a repository URL such as:
https://github.com/username/git-demo.gitThis is the address your local Git repository will use to communicate with GitHub.
Using git remote add
Inside your local project, run:
git remote add origin https://github.com/username/git-demo.gitYou can read the command like this:
git remote add → Add a remote repository → origin → Call this remote "origin" → https://... → GitHub repository URL
After this command, Git knows that:
origin = this GitHub repositoryChecking the Remote
Run:
git remoteand you will usually see:
originTo see the actual remote URL, use:
git remote -vYou may see:
origin https://github.com/username/git-demo.git (fetch)
origin https://github.com/username/git-demo.git (push)The fetch URL is used when getting data from the remote, while the push URL is used when sending data to it. For basic setups, they are usually the same address.
If You Used git clone
If you cloned the project with:
git clone https://github.com/username/git-demo.gitGit normally configured origin automatically.
You do not need to run:
git remote add origin ...again.
Just use:
git remote -vto check the existing configuration.
KNOWLEDGE QUIZ
git remote add origin URL do?
HTTPS and SSH
GitHub commonly provides two kinds of repository URLs.
HTTPS:
https://github.com/username/git-demo.gitSSH:
[email protected]:username/git-demo.gitBoth work.
For beginners, HTTPS is often easier to understand. SSH requires an SSH key setup, which can be learned later.
This tutorial will use HTTPS for the basic examples.
Fixing an Incorrect Remote URL
If origin points to the wrong repository, update it with:
git remote set-url origin https://github.com/username/correct-repo.gitThen check again:
git remote -vto make sure the URL is correct.
Practice
A local repository must connect to https://github.com/example/notes.git. Write commands to add origin, inspect its fetch/push URLs, and fix the URL if it was entered incorrectly.