You’ve created a project on your local machine, you’ve initialized it as a git repository, and you’re coding along happily.
Now you’ve decided that you’d like to save a copy on GitHub. Maybe so that you can collaborate with others more easily? Maybe just so that you can save it remotely? Whatever the reason, how do you do it?
It’s easy!
-
Create a blank GitHub repository like you normally would by clicking one of the “New” buttons you can find in various places on the website after you’ve signed in.
-
Give your repository a name that makes sense for your project, write a description if you want, and to avoid merge conflicts, leave “Add a README file” and “Add .gitignore” unchecked for now. It’s probably better to just create those on your own at this point if they don’t already exist for your project. Then click on “Create repository” at the bottom of the form.
-
The best part! GitHub already has instructions here for you on exactly what to do next on your terminal command line! In fact, although we didn’t cover it here, there are even instructions on what to do if you don’t even have a repository at all and how to create one right now if you had not already done so.
So let’s go ahead and use the URL that is provided by GitHub (as you can see in the screenshot) for your new online repository and run those three lines in the terminal from the root directory of your local repository. They’ve made it so easy that you don’t even have to plug in the URL; you can simply copy and paste it. So in my case, it would look like this with the URL for the repo I’ve just made:
git remote add origin https://github.com/clendon/practice-repo.git git branch -M master git push -u origin master
If during step 3, you get the error fatal: remote origin already exists.
, it’s because your local repository is already associated with a different online repository, but we can remove that association and fix it easily. Do the following:
git remote remove origin
And then attempt step 3 again and that should do the trick.
And there you go, should now be able to push
and pull
from/to your remote repository on GitHub.
Top comments (0)