Pushing a local repository to GitHub

One of Git’s strengths is the ability to configure a repository locally, so you can dive right into an idea regardless of whether or not you have a reliable (and secure) internet connection.

Once you’re ready to share your code, one of the easiest and cheapest ways to do so is to import this local repository to GitHub. Here’s how:

  1. Log into your GitHub account. If you don’t already have one, sign up for free.
  2. Click the “+” in the upper-right corner of any page and select “New repository”.
  3. Enter a name (and optional description) for your repository.
  4. If you have a paid account and you don’t want your code to be visible to the GitHub community, click private. Otherwise leave public selected.
  5. Leave Initialize this repository with a README unchecked.
  6. Click Create repository.
  7. When the page reloads, copy the repository URL. It will follow this format:
  8. Open a terminal window and navigate to the directory containing your local repository.
  9. If you haven’t already initiated a Git repository, do so with this command:
    git init
  10. Set the remote origin for your local repository to point to the repository you just set up on Github:
    git remote add origin https://github.com/[username]/[repository-name].git

    If it’s a private repository associate your username with the repository in this command to save yourself some typing:

    git remote add origin https://[username]@github.com/[username]/[repository-name].git
  11. Finally, push the contents of your local repository to GitHub:
    git push -u origin master

Your project is now on GitHub. In addition, your local and GitHub repositories are now connected and can be kept current to one another with the push or pull commands (depending on the direction in which the changes are moving).