Sub topic 6
Connecting the local repository with git remote add
This chapter assumes you have a local repository with at least one commit, an empty repository on GitHub, and its URL (HTTPS or SSH) with matching authentication already set up. If git status shows a clean working tree on a branch with commits, you are ready.
What the command does
git remote add writes a name and a URL into your repository's configuration file, .git/config. It makes no network connection and sends no data. It only records a shortcut, so later commands can say origin instead of a full URL.
The syntax is:
git remote add <name> <url>Here <name> is any label you choose. By convention the first remote is called origin, which is also the name git clone picks for you. Git gives the name no special meaning.
Adding the remote
Run this from the root of your local repository. Replace the placeholders with your own username and repository name.
git remote add origin [email protected]:YOUR-USERNAME/YOUR-REPO.git
git remote -vThe second command lists each remote with its URLs. Expect output like this:
origin [email protected]:YOUR-USERNAME/YOUR-REPO.git (fetch)
origin [email protected]:YOUR-USERNAME/YOUR-REPO.git (push)Fetch and push URLs are listed separately because Git allows them to differ. In this setup they are the same.
If you chose HTTPS, the URL has the form https://github.com/YOUR-USERNAME/YOUR-REPO.git. Everything else in this chapter stays the same.
Checking the result
git remote -v only shows what you typed. It does not prove the URL is correct or that you can reach it. To test the connection, ask the remote for its branches:
git ls-remote originFor a new, empty GitHub repository this prints nothing and exits successfully. An error here means the URL or your authentication is wrong, and it is better to find that now than during your first push. A "Repository not found" message usually means a typo in the username or repository name, or a private repository that your credentials cannot see.
Common mistakes
- Running the command twice. Git refuses with
error: remote origin already exists.Either pick a different name, or change the existing remote's URL withgit remote set-url origin <url>. - Typing the name and URL in the wrong order. The name comes first. If you swap them, Git creates a remote named after the URL, and
git remote -vshows something odd. - Running it outside the repository. You get
fatal: not a git repository. Change into the project directory first. - Copying a URL with a trailing space or missing
.git. A missing.gitsuffix works on GitHub, but a stray space breaks the URL.
You can view the stored configuration directly if you want to confirm what was written:
git config --get remote.origin.urlThis prints only the URL for the remote named origin.
Where this leads
At this point your local repository knows where GitHub is, but no commits have moved. The next step is to make sure your branch name matches the one you want on GitHub, and then push. Until you push, the GitHub page for your repository still shows the empty-repository instructions.
Exercise
You run git remote add origin [email protected]:me/app.git and Git prints nothing. Then git remote add origin https://github.com/me/app.git fails with "remote origin already exists". What does the first command's silence mean, and what single command switches origin to the HTTPS URL?
Show answer
Silence means success: the entry was written to .git/config, and no network request was made. To switch URLs, run git remote set-url origin https://github.com/me/app.git, and confirm with git remote -v.
Exercise
After adding a remote to a new repository, git branch -r lists nothing. Is something wrong?
Show answer
No. Remote-tracking branches such as origin/main are created only after a fetch or push talks to the remote. Adding the remote only stores its name and URL.