Blog
3 min read

git remote add origin: Connect Your Project to GitHub

What a Git remote is, what 'origin' means, and how to connect a local project to a new GitHub repository with git remote add origin and git push -u. Plus how to view, change and remove remotes, and fix 'remote origin already exists'.

You've built something on your computer, it's a Git repository, and now you want it on GitHub. The command that connects the two is git remote add origin.

What's a remote?

A remote is a named link to another copy of your repository — usually on GitHub, GitLab or a server. Your local repository can have several. (Git and GitHub for beginners)

origin is just the conventional name for the main one. There's nothing special about the word; git clone uses it by default, so everyone does.

Connecting a local project to a new GitHub repo

1. Create an empty repository on GitHub. Don't tick "Add a README" — an empty repo avoids a conflict on your first push. (Refusing to merge unrelated histories)

2. In your project folder, make sure it's a Git repo with at least one commit:

git init
git add .
git commit -m "First commit"

3. Add the remote:

git remote add origin https://github.com/you/my-app.git
# or with SSH:
git remote add origin git@github.com:you/my-app.git

4. Push, and set up tracking:

git branch -M main
git push -u origin main
  • git branch -M main renames your branch to main (some setups start with master).
  • -u sets upstream tracking, so from now on plain git push and git pull know where to go.

Done — refresh GitHub and your code is there.

Viewing your remotes

git remote -v
origin  git@github.com:you/my-app.git (fetch)
origin  git@github.com:you/my-app.git (push)

"error: remote origin already exists"

You (or a tool, or git clone) already added an origin. Either change its URL:

git remote set-url origin git@github.com:you/my-app.git

or remove it and add it again:

git remote remove origin
git remote add origin git@github.com:you/my-app.git

Other errors on the first push

  • src refspec main does not match any — there's no commit yet, or your branch isn't called main. (Fix it)
  • Permission denied (publickey) — your SSH key isn't set up with GitHub. (Fix it)
  • Updates were rejected because the remote contains work — the GitHub repo isn't empty (it has a README or licence). (Fix it)
  • Authentication failed over HTTPS — GitHub doesn't accept your account password for Git. Log in through the browser prompt, gh auth login, or use a personal access token, or switch to SSH. (SSH keys explained)
  • fatal: not a git repository — you're in the wrong folder, or haven't run git init. (Fix it)

Switching from HTTPS to SSH (or back)

git remote set-url origin git@github.com:you/my-app.git

Same command for moving a repository to a new location — say, after renaming it or moving it to an organisation.

More than one remote

Common setups:

  • origin + upstream — your fork and the original project. (Fork vs clone)
  • origin + a server — push to GitHub and to a deploy server.
git remote add upstream https://github.com/team/app.git
git fetch upstream

The summary

git remote add origin <url>     # connect
git push -u origin main         # first push, with tracking
git remote -v                   # check
git remote set-url origin <url> # change

EasySpawn connects your server to your Git repository so pushes deploy and your project's history lives with the running app. See how it works or join the waitlist.

Related: Git and GitHub for Beginners · git clone Explained · Git Push Rejected · SSH Keys Explained

Keep reading