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
-
remote repo url should something like this?
https://example.com/projects/luotsi/repositories/git/marketing-site-redux
– Om3gaJun 25, 2012 at 11:54 -
2Probably, yes. It usually ends with something like
https://example.com/.../reponame.git
, although the.git
ending is not technically required.– vergenztJun 25, 2012 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.– vergenztJun 25, 2012 at 11:57
-
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.– vergenztJun 25, 2012 at 12:01 -
4
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
-
You allegedly wrote the referenced Medium article. Why is "GitHub" misspelt as "Github" in the first half, but not in the second half? Mar 7 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 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
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