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.
-
Create a new empty repository on GitHub and don't thick any boxes that will already add files to your repository.
-
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. -
Open a terminal.
-
Run the following command in the terminal to create a new Git repository.
git init
-
To stage all files of your project before committing run the following command.
git add .
-
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"
-
Run the following command to rename the local branch from
master
tomain
. We have to do this because the default master branch on GitHub is calledmain
, but the default branch created with thegit init
command is calledmaster
.git branch --move main
OR you could also use a shortcut with the following command.
git branch -M main
-
Copy the remote URL of your repository on GitHub.
-
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
-
To finally push your files to your repository use the following command.
git push -u origin main