Add a Local Git Repository to GitHub

clendon

Clendon DuRapau

Posted on March 1, 2022

Add a Local Git Repository to GitHub

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!

  1. 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.

    screenshot of new repo button

  2. 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.

    screenshot of create new repo form

  3. 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.

    screenshot of repo quick setup page

    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
Enter fullscreen mode Exit fullscreen mode

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.

💖 💪 🙅 🚩
clendon
Clendon DuRapau

Posted on March 1, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related