Connect a local repository with a remote repository
GitRepositoryGit Problem Overview
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?
Git Solutions
Solution 1 - Git
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
Solution 2 - Git
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
Solution 3 - Git
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
Solution 4 - Git
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