Github – Create a repository from existing directory

  1. enter terminal of the directory, run:
git init -b main

The message will be like:

Initialized empty Git repository in your path/.git/
  1. stage all the files in the project:
git add .
  1. commit all the files in the project:
git commit -m "First commit"
  1. create a repository from github web console with your github account

  2. add the url of the remote repository you just created to the lcoal repository

git remote add origin  <REMOTE_URL> 

Verifies the new remote URL

git remote -v
git push origin main

you may receive the eroor like:

Updates were rejected because the remote contains work that you do not have locally

In this case, that is because when we are trying to push to remote repository but has created a new file on remote which has not been pulled yet

One solution is to use force push:

git push -f origin main

But this is really dangerous, because it will overwrite the history that is in remote to keep the remote history like your local copy.

A more safe solution will be:

  1. Pull from the remote to keep you local repository updated
git pull origin main
  1. Then push the local repository remotely
git push origin main