2 min read

Guide: Initializing a Git Repo and Pushing to GitHub

This guide walks through the standard workflow for taking a local project folder and publishing it to a new repository on GitHub.

1. Prerequisites

2. On GitHub: Create the Remote Repository

  1. Log in to GitHub.com.
  2. Click the + icon in the top-right corner and select New repository.
  3. Give your repository a name (e.g., my-new-project).
  4. Crucial Step: Leave the repository empty. Do NOT check "Add a README file," "Add .gitignore," or "Choose a license." This prevents conflicts on your first push.
  5. Click Create repository.
  6. On the next page, under "…or push an existing repository from the command line," copy the repository URL. It will look like https://github.com/YourUsername/my-new-project.git.

3. On Your Local Machine: Initialize the Project

Open your terminal and navigate into your project's root folder.

  1. Initialize Git:

    • This creates a hidden .git folder where all version history is stored.
      git init
  2. Stage Your Files:

    • This prepares all files in the current directory for the first commit.
      git add .
  3. Create the First Commit:

    • This creates a snapshot of your project at this point in time.
      git commit -m "Initial commit"

4. Connect Local to Remote and Push

Now, link your local repository to the one you created on GitHub and send your code.

  1. Add the Remote:
    • Replace the URL with the one you copied from GitHub.
      git remote add origin https://github.com/YourUsername/my-new-project.git
  2. Push Your Code:
    • The -u flag sets the upstream tracking branch, so in the future, you can just type git push.
      git push -u origin main

After the push completes, refresh your repository page on GitHub. Your files will now be there.