I have a local repository. I created the whole application, but now I want to push it to a remote repository. I already have remote repo as well. How can I connect these two repositories without losing any work that I did?
4 Answers
Use:
git remote add origin <remote_repo_URL>
git push --all origin
If you want to set all of your branches to automatically use this remote repository when you use git pull
, add --set-upstream
to the push:
git push --all --set-upstream origin

- 30,738
- 21
- 105
- 131

- 9,669
- 4
- 40
- 47
-
1remote repo url should something like this? `https://example.com/projects/luotsi/repositories/git/marketing-site-redux` – Om3ga Jun 25 '12 at 11:54
-
2Probably, yes. It usually ends with something like `https://example.com/.../reponame.git`, although the `.git` ending is not technically required. – vergenzt Jun 25 '12 at 11:56
-
The easiest way to find out is to try it and see. Git will let you know if it's not right. – vergenzt Jun 25 '12 at 11:57
-
I did it it give me this error `fatal: Not a git repository (or any of the parent directories): .git` – Om3ga Jun 25 '12 at 11:59
-
2Is your app in a local repository yet (i.e. have you at some point done `git init` and made commits and stuff)? That error message means you're not in a repository. – vergenzt Jun 25 '12 at 12:01
-
4go one folder level down, your repo is in another castle – Chris McCall May 17 '13 at 22:52
A one line exact answer is provided by vergenzt, but if you want to go through the details on how to commit your code and connect a local and remote GitHub repository and push code into the remote repository using Git CLI, you can go through this article: Beginners guide to Git using CLI

- 30,738
- 21
- 105
- 131

- 757
- 5
- 11
-
*You* allegedly wrote the referenced Medium article. Why is "GitHub" misspelt as "Github" in the first half, but not in the second half? – Peter Mortensen Mar 07 '22 at 22:32
First make this command:
git remote add origin {URL for the remote repository}
Then tab this command:
git push origin master
Note the name of master is the name of the remote repository on GitHub. If it has another name, like main or something else, make it main instead of master.
It's expected to connect and push your local repo now. If something happens or any error appear, try this command:
git pull --rebase origin master

- 65
- 1
- 6

- 19
- 9
If you have
- your local project already tracked by git,
- an empty remote repository existing, which you want to contain the project,
do the following steps:
cd existingLocalRepo
git remote set-url origin <remote_repo_URL>
git push -u origin --all
Explanation:
- navigate to your local repo
- tell git where the remote repo is located
- upload/push your local branches to the remote repo

- 1,280
- 2
- 11
- 34