How to convert existing non-empty directory into a Git working directory and push files to a remote repository

Solution 1:

Given you've set up a git daemon on <url> and an empty repository:

cd <localdir>
git init
git add .
git commit -m 'message'
git remote add origin <url>
git push -u origin master

Solution 2:

This is how I do. I have added an explanation to understand what the heck is going on.

Initialize Local Repository

  • first, initialize Git with

    git init

  • Add all Files for version control with

    git add .

  • Create a commit with a message of your choice

git commit -m 'AddingBaseCode'

Initialize Remote Repository

  • Create a project on GitHub and copy the URL of your project. as shown below:

enter image description here

Link Remote repo with Local repo

  • Now use copied URL to link your local repo with the remote GitHub repo. When you clone a repository with git clone, it automatically creates a remote connection called origin pointing back to the cloned repository. The command remote is used to manage a set of tracked repositories.

    git remote add origin https://github.com/hiteshsahu/Hassium-Word.git

Synchronize

  • Now we need to merge local code with remote code. This step is critical otherwise we won't be able to push code on GitHub. You must call 'git pull' before pushing your code.

    git pull origin master --allow-unrelated-histories

Commit your code

  • Finally, push all changes on GitHub

    git push -u origin master

Note: Now Github uses "main" as the default branch. If your project use "main" instead of "master simply replace "master" with "main" from the above commands