How to push an existing project to GitHub?

Sometimes I want to push a project to GitHub. But often when I do that my project is somehow nested in some folders on GitHub instead of having the root of my project there. That's why I decided to figure out how to make sure that won't happen anymore and how I'll always be able to push a fresh project to GitHub.

  1. Create a new empty repository on GitHub and don't thick any boxes that will already add files to your repository.
  2. Go to the project on your file system and add a .gitignore file to the root of your project. On GitHub you can find some useful templates for .gitignore files.
  3. Open a terminal.
  4. Run the following command in the terminal to create a new Git repository.
git init
  1. To stage all files of your project before committing run the following command.
git add .
  1. To commit all staged files run the following command after replace WRITE YOUR OWN MESSAGE with your own commit message.
git commit -m "WRITE YOUR OWN MESSAGE"
  1. Run the following command to rename the local branch from master to main. We have to do this because the default master branch on GitHub is called main, but the default branch created with the git init command is called master.
git branch --move main

OR you could also use a shortcut with the following command.

git branch -M main
  1. Copy the remote URL of your repository on GitHub.

  2. Run the following command to add a remote to the repository so git knows to what remote you want to push your files. Replace the example remote URL with your own URL.

git remote add origin https://github.com/IkBenDeSjaak/EXAMPLE.git
  1. To finally push your files to your repository use the following command.
git push -u origin main