git remote add origin: connect a local repo to GitHub
By Flavio
Published · Updated
Tagged: Git
git remote add origin connects a local repo to GitHub. Check it with git remote -v, change a taken origin with set-url, and push with git push -u origin main.
git remote add origin saves a GitHub URL on your local repository, under the name origin.
The name is a label on your machine. The URL is what Git contacts when you fetch or push. Nothing is uploaded until you push.
You need git remote add when you created the repository locally with git init, then created an empty repository on GitHub afterwards. git clone skips this step, because clone writes origin for you.
Add origin
From the project folder, point origin at the GitHub repository:
git remote add origin git@github.com:flaviocopes/myrepo.git
Change flaviocopes/myrepo to your repository. GitHub also offers an HTTPS URL, https://github.com/flaviocopes/myrepo.git. Use the form that matches how you log in.
The command writes the URL into .git/config. It does not open a connection, and it does not send commits.
You can pick a name other than origin. A fork often uses upstream for the original repository and origin for your copy. The rest of this page uses origin, because that is the name in GitHub’s first-push instructions.
Check it with git remote -v
git remote -v
A new remote shows the same URL twice. One line is fetch, one line is push:
origin git@github.com:flaviocopes/myrepo.git (fetch)
origin git@github.com:flaviocopes/myrepo.git (push)
git remote get-url origin prints the URL on its own. No output from git remote -v means you have not added a remote yet.
When origin already exists
Run git remote add a second time and Git refuses:
error: remote origin already exists.
Stop there. Read the URL you already have with git remote -v. Do not delete the remote just to add it again.
If the repository moved, or the first URL was wrong, replace it:
git remote set-url origin git@github.com:flaviocopes/myrepo.git
set-url changes the URL and leaves your commits where they are. Check the result with git remote -v.
A typo does not fail at git remote add. It fails on the next push, and GitHub answers ERROR: Repository not found. Fix the URL with git remote set-url and push again.
Push for the first time
The GitHub repository has to exist before this, and the local branch needs at least one commit. Then:
git push -u origin main
-u remembers origin/main as the upstream of your local main. The next git push and git pull can omit the remote and the branch name.
Use the name of your local branch. If that branch is not main, put its name in the command instead.
A push before any commit fails on your machine:
error: src refspec main does not match any
Commit on main, then run git push -u origin main again.
Put origin back after you removed it
I had removed origin from a Git repository with git remote rm origin. To reconnect the same local repository to GitHub, I added the remote again:
git remote add origin git@github.com:flaviocopes/myrepo.git
Removing a remote deletes that configuration. Your local commits stay.
The same commands are a lesson in the free Git course if you are walking through the course in order.
Want me to talk about your product? You can sponsor this site.
Related posts about git: