Connect a local repository with a remote repository

GitRepository

Git 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:

  1. navigate to your local repo
  2. tell git where the remote repo is located
  3. upload/push your local branches to the remote repo

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionOm3gaView Question on Stackoverflow
Solution 1 - GitvergenztView Answer on Stackoverflow
Solution 2 - GitUSMAN FAZILView Answer on Stackoverflow
Solution 3 - Githamza houriView Answer on Stackoverflow
Solution 4 - GitSparkofskaView Answer on Stackoverflow