How to add an existing project to GitHub
I have found that a lot of my coding journey has been going back to my notes from the earlier days to look back on how to do the simplest thing. This week, in my eagerness to get started on a new project, I jumped right in with “create-react-app” and then realised that I had not created the repo first on GitHub. So back to the notes to remember how to do it! Now I have made this guide with simple steps to follow which will help future me and hopefully you too!
- Go onto GitHub and start a new project. Give your project a name, decide whether it is public or private and you are good to go! I would recommend not initialising the new repository with a README, .gitignore or a license to avoid errors. You can easily add this after your project has been pushed to GitHub either on GitHub or in your code editor.
2. Navigate to the project folder that you want to track in the CLI, then type: Open the project folders CLI and initialise the local directory with this command:
$ git init
3. Stage the files in the local repo to be committed.
$ git add .
4. Commit the files that you’ve staged in your local repository. This prepares the files to be pushed to the remote repo:
$ git commit -m "First commit"
5. Now go back onto GitHub and you should see this:
We are trying to “push an existing repository from the command line” so copy that code
6. Paste it into your projects local CLI:
$ git remote add origin <repo-URL>
# this defines where your local repo will be pushed$ git push -u origin master
# this pushes the changes in your local repo to GitHub.
Congratulations! Your project is now on GitHub!
If you are are going to continue working on the repo then I would recommend immediately changing to a new branch so that you can easily track you project and easily revert to previous versions.
$ git checkout -b <new-branch-name>
To push and subsequent changes to the local repo all you need to do is:
$ git add .
$ git commit -m "a descriptive message for you to track the changes"
$ git push -u origin master
Go onto GitHub and merge the changes and you are good to go!